<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

125. Shtirlits

time limit per test: 0.25 sec.
memory limit per test: 4096 KB



There is a checkered field of size N x N cells (1 Ј N Ј 3). Each cell designates the territory of a state (i.e. N2 states). Each state has an army. Let A [i, j] be the number of soldiers in the state which is located on i-th line and on j-th column of the checkered field (1£i£N, 1£j£N, 0 £  A[i, j] £  9). For each state the number of neighbors, B [i, j], that have a larger army, is known. The states are neighbors if they have a common border (i.e. 0 £  B[i, j]  £  4). Shtirlits knows matrix B. He has to determine the number of armies for all states (i.e. to find matrix A) using this information for placing forces before the war. If there are more than one solution you may output any of them.



Input

The first line contains a natural number N. Following N lines contain the description of matrix B - N numbers in each line delimited by spaces.


Output

If a solution exists, the output file should contain N lines, which describe matrix A. Each line will contain N numbers delimited by spaces. If there is no solution, the file should contain NO SOLUTION.


Sample Input

3
1 2 1
1 2 1
1 1 0
Sample Output

1 2 3
1 4 5
1 6 7
Author	: Victor G. Samoilov, Herman "Smash" Narkaytis
Resource	: 5th Southern Subregional Contest. Saratov 2002
Date	: 2002-10-10

<|response|>
1. Abridged Problem Statement  
We have an N×N grid (1≤N≤3). Each cell (i,j) must be assigned an integer A[i][j] between 0 and 9.  For each cell we are given B[i][j], the exact number (0–4) of its orthogonal neighbors whose A-value is strictly larger than A[i][j].  Recover any matrix A consistent with B, or report “NO SOLUTION” if none exists.

2. Key Observations  
- Since N≤3, there are at most 9 cells and each A[i][j] ∈ {0,…,9}.  A brute-force DFS over all assignments is feasible if we prune aggressively.  
- We only care about comparisons between neighbors.  When we assign a value to a cell, we can immediately update the “remaining larger-neighbor count” for already-assigned neighbors (up and left) and for the current cell itself.  
- If at any point the remaining count for some cell goes negative, or if we finish assigning all of a cell’s neighbors and its remaining count ≠0, we can backtrack.  

3. Full Solution Approach  
a. Read N and the matrix B.  Make a copy b2 = B, which will track how many larger neighbors each cell still needs as we assign A.  
b. Maintain an N×N array A of assignments, initially unset.  We will fill it in row-major order, pos=0…N*N−1, where pos→(i=pos/N, j=pos%N).  
c. Define an update(i,j,delta) routine that applies delta=−1 or +1 when placing or removing A[i][j]:  
   - Look at the two already-assigned neighbors: (i−1,j) and (i,j−1), if they exist.  
   - If neighbor’s A > A[i][j], then that neighbor contributed one to B[i][j], so we add delta to b2[i][j].  
   - If A[i][j] > neighbor’s A, then the current cell contributed one to B[neighbor], so we add delta to b2[neighbor].  
   - If any touched b2 entry becomes negative, update() reports failure.  
d. Backtracking DFS(pos):  
   - If pos==N*N, all cells are assigned.  At that point, the only un-checked cells are those in the last row—their “down” neighbors do not exist, so all their neighbors have been assigned.  We must check b2[last row][*] are all zero.  If so, print A and exit; otherwise backtrack.  
   - Otherwise let (i,j)=pos.  Try A[i][j]=0,…,9:  
     1. Call update(i,j,−1).  If it fails (some b2<0), undo with update(i,j,+1) and continue.  
     2. If i>0 (there is an “up” neighbor), that neighbor’s last missing neighbor was (i,j).  Now that we’ve placed A[i][j], b2[i−1][j] must be exactly zero; if not, skip this value.  
     3. Recurse DFS(pos+1).  
     4. Upon return, undo with update(i,j,+1).  
e. If DFS completes without printing a solution, output “NO SOLUTION.”  

This explores at most 10^(N²) assignments in the worst case but in practice is extremely fast for N≤3 thanks to early pruning.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

int n;
// A will hold the final army sizes; b2 tracks remaining “larger-neighbor” needs.
int A[3][3], b2[3][3];

// Attempt to adjust b2 when placing/removing A[i][j].
// delta = -1 for placement, +1 for removal.
// Returns true if any b2 entry goes negative (i.e. a failure to satisfy B).
bool update(int i, int j, int delta) {
    // Check the “up” neighbor (i-1,j), if any
    if (i > 0) {
        // If neighbor’s value > current, it contributed to B[i][j]
        if (A[i-1][j] > A[i][j]) {
            b2[i][j] += delta;
        }
        // If current > neighbor, current contributed to B[i-1][j]
        if (A[i][j] > A[i-1][j]) {
            b2[i-1][j] += delta;
        }
        if (b2[i][j] < 0 || b2[i-1][j] < 0) return true;
    }
    // Check the “left” neighbor (i,j-1), if any
    if (j > 0) {
        if (A[i][j-1] > A[i][j]) {
            b2[i][j] += delta;
        }
        if (A[i][j] > A[i][j-1]) {
            b2[i][j-1] += delta;
        }
        if (b2[i][j] < 0 || b2[i][j-1] < 0) return true;
    }
    return false;
}

// Recursive backtracking over the flat index pos = 0..n*n-1
void dfs(int pos) {
    if (pos == n*n) {
        // All cells assigned.  Ensure the last row's b2[*] are zero.
        for (int j = 0; j < n; j++) {
            if (b2[n-1][j] != 0) return;
        }
        // Found a valid solution: print and exit.
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                cout << A[i][j] << (j+1<n ? ' ' : '\n');
            }
        }
        exit(0);
    }
    int i = pos / n, j = pos % n;
    // Try all values 0..9 for A[i][j]
    for (int v = 0; v <= 9; v++) {
        A[i][j] = v;
        // Place it: reduce b2 counts
        if (!update(i, j, -1)) {
            // If there is an "up" neighbor, its last neighbor to assign was us,
            // so now it must have exactly b2=0.
            if (i==0 || b2[i-1][j]==0) {
                dfs(pos+1);
            }
        }
        // Undo placement
        update(i, j, +1);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    // Read B into b2 initially
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> b2[i][j];

    dfs(0);
    // If no solution was printed:
    cout << "NO SOLUTION\n";
    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys
sys.setrecursionlimit(10000)

def main():
    data = sys.stdin.read().split()
    n = int(data[0])
    B = []
    idx = 1
    for _ in range(n):
        B.append(list(map(int, data[idx:idx+n])))
        idx += n

    # A will store the army sizes; b2 the remaining larger-neighbor counts
    A = [[0]*n for _ in range(n)]
    b2 = [row[:] for row in B]

    # update(i,j,delta): apply delta=-1 to place A[i][j], +1 to remove.
    # Returns False if any b2 becomes negative.
    def update(i, j, delta):
        # Check the up and left neighbors only
        for di, dj in [(-1,0),(0,-1)]:
            x, y = i+di, j+dj
            if 0 <= x < n and 0 <= y < n:
                if A[x][y] > A[i][j]:
                    b2[i][j] += delta
                if A[i][j] > A[x][y]:
                    b2[x][y] += delta
                if b2[i][j] < 0 or b2[x][y] < 0:
                    return False
        return True

    def dfs(pos):
        if pos == n*n:
            # All cells done: check last row demands exactly zero
            if all(b2[n-1][j] == 0 for j in range(n)):
                for row in A:
                    print(" ".join(map(str,row)))
                sys.exit(0)
            return

        i, j = divmod(pos, n)
        for v in range(10):
            A[i][j] = v
            if update(i, j, -1):
                # If there's an up-neighbor, it has no more unknown neighbors now
                if i==0 or b2[i-1][j] == 0:
                    dfs(pos+1)
            update(i, j, +1)

    dfs(0)
    print("NO SOLUTION")

if __name__ == "__main__":
    main()
```