#C3022. Magic Square Completion

    ID: 46404 Type: Default 1000ms 256MiB

Magic Square Completion

Magic Square Completion

Anna has a partially filled square matrix of size \(n\). Your task is to fill in the blank cells (denoted by 0) so that each row, each column, and both the main and secondary (anti) diagonals contain every integer from \(1\) to \(n\) exactly once. Some cells are pre‐filled and must remain unchanged. If there are multiple valid solutions, output any one of them.

Input Format: The input begins with an integer \(n\) on the first line, followed by \(n\) lines each containing \(n\) space-separated integers. A value of 0 indicates an empty cell.

Output Format: Print the completed square matrix in \(n\) lines. Each line should contain \(n\) space-separated integers. The solution must ensure that every row, every column, and both the main and secondary diagonals are permutations of \(\{1, 2, \dots, n\}\).

inputFormat

The first line contains a positive integer (n). Each of the following (n) lines contains (n) space-separated integers representing a row of the matrix. A cell containing 0 is empty and needs to be filled.

outputFormat

Output the completed matrix with (n) lines, where each line contains (n) space-separated integers. The filled matrix must be such that each row, each column, and both the main and the anti diagonal contain all integers from 1 to (n) exactly once.## sample

3
1 0 3
0 2 0
0 0 0
1 2 3

3 2 1 2 1 3

</p>