#K95072. Puzzling Grid Rotation

    ID: 38782 Type: Default 1000ms 256MiB

Puzzling Grid Rotation

Puzzling Grid Rotation

You are given an \(N \times N\) grid of integers. Your task is to examine every possible contiguous subgrid of size \(m \times m\) (with \(m \ge 2\)) contained in the grid. For each subgrid, if every element located on its border is nonzero, then rotate that entire subgrid by 90° clockwise in-place.

The border of a subgrid consists of the elements in the first and last rows and the first and last columns of the subgrid. Mathematically, if a subgrid starts at position \((i,j)\) and has dimension \(m\), then its border is:

[ { grid[i][j], grid[i][j+1], \ldots, grid[i][j+m-1] } \cup { grid[i+m-1][j], grid[i+m-1][j+1], \ldots, grid[i+m-1][j+m-1] } \cup { grid[i][j], grid[i+1][j], \ldots, grid[i+m-1][j] } \cup { grid[i][j+m-1], grid[i+1][j+m-1], \ldots, grid[i+m-1][j+m-1] }. ]

If the entire border is nonzero, perform a 90° clockwise rotation on that \(m \times m\) subgrid. Process all possible subgrids (considering all valid positions and all \(m\) from 2 up to \(N\)). After processing, output the final state of the grid.

inputFormat

The input will be read from standard input (stdin). The first line contains an integer \(N\), representing the dimensions of the grid. The following \(N\) lines each contain \(N\) space-separated integers representing the grid rows.

outputFormat

Output the final state of the grid to standard output (stdout) in the same format: \(N\) lines, each containing \(N\) space‐separated integers.

## sample
5
1 2 3 4 5
1 0 0 0 1
1 0 8 0 1
1 0 0 0 1
1 2 3 4 5
1 2 3 4 5

1 0 0 0 1 1 0 8 0 1 1 0 0 0 1 1 2 3 4 5

</p>