#include #define rand() dis(gen) using namespace std; int i, j, n, a[30][30], N, cnt; bool ok = 1; mt19937 gen(time(0)); uniform_int_distribution dis(0, 1e6); void solve(int x, int y) { if(y == n * n) { solve(x + 1, 0); return; } if(x == n * n) { ok = 1; for(int i = 0; i < N; ++i) for(int j = 0; j < N; ++j) cout << a[i][j] << " \n"[j == N - 1]; return; } if(a[x][y]) { solve(x, y + 1); return; } int freq[N + 5]; memset(freq, 0, sizeof(freq)); for(int i = 0; i < N; ++i) { if(a[x][i]) ++freq[a[x][i]]; if(a[i][y]) ++freq[a[i][y]]; } for(int i = x - (x % n); i < x - (x % n) + n; ++i) for(int j = y - (y % n); j < y - (y % n) + n; ++j) if(a[i][j]) ++freq[a[i][j]]; vector v; for(int i = 1; i <= N; ++i) if(!freq[i]) v.push_back(i); while(v.size()) { int poz = rand() % v.size(); a[x][y] = v[poz]; solve(x, y + 1); if(ok) return; a[x][y] = 0; swap(v[poz], v[v.size() - 1]); v.pop_back(); } } int main() { ios_base::sync_with_stdio(0); cin >> n; N = n * n; for(i = 0; i < N; ++i) for(j = 0; j < N; ++j) { cin >> a[i][j]; if(!a[i][j]) ++cnt; } while(1) { bool yes = 0; for(i = 0; i < N; ++i) for(j = 0; j < N; ++j) { if(a[i][j]) continue; int x = i, y = j; int freq[N + 5]; memset(freq, 0, sizeof(freq)); for(int ii = 0; ii < N; ++ii) { if(a[x][ii]) ++freq[a[x][ii]]; if(a[ii][y]) ++freq[a[ii][y]]; } for(int ii = x - (x % n); ii < x - (x % n) + n; ++ii) for(int jj = y - (y % n); jj < y - (y % n) + n; ++jj) if(a[ii][jj]) ++freq[a[ii][jj]]; vector v; for(int ii = 1; ii <= N; ++ii) if(!freq[ii]) v.push_back(ii); if(v.size() != 1) continue; a[i][j] = v[0]; i = 1e9; j = 1e9; } if(i < 1e9 && j < 1e9) { ok = 0; break; } } if(!ok) solve(0, 0); else { for(i = 0; i < N; ++i) for(j = 0; j < N; ++j) cout << a[i][j] << " \n"[j == N - 1]; } return 0; }