#P1986B. Matrix Stabilization
Matrix Stabilization
No submission language available for this problem.
Description
You are given a matrix of size , where the rows are numbered from to from top to bottom, and the columns are numbered from to from left to right. The element at the intersection of the -th row and the -th column is denoted by .
Consider the algorithm for stabilizing matrix :
- Find the cell such that its value is strictly greater than the values of all its neighboring cells. If there is no such cell, terminate the algorithm. If there are multiple such cells, choose the cell with the smallest value of , and if there are still multiple cells, choose the one with the smallest value of .
- Set .
- Go to step .
In this problem, cells and are considered neighbors if they share a common side, i.e., .
Your task is to output the matrix after the stabilization algorithm has been executed. It can be shown that this algorithm cannot run for an infinite number of iterations.
Each test consists of multiple sets of input data. The first line contains a single integer () — the number of sets of input data. This is followed by their description.
The first line of each set of input data contains two integers and () — the number of rows and columns of matrix .
The next lines describe the corresponding rows of the matrix. The -th line contains integers ().
It is guaranteed that the sum of over all sets of input data does not exceed .
For each set of input data, output lines with numbers in each line — the values of the cells of matrix after the stabilization algorithm.
Input
Each test consists of multiple sets of input data. The first line contains a single integer () — the number of sets of input data. This is followed by their description.
The first line of each set of input data contains two integers and () — the number of rows and columns of matrix .
The next lines describe the corresponding rows of the matrix. The -th line contains integers ().
It is guaranteed that the sum of over all sets of input data does not exceed .
Output
For each set of input data, output lines with numbers in each line — the values of the cells of matrix after the stabilization algorithm.
Note
In the first set of input data, the algorithm will select the cell twice in a row and then terminate.

In the second set of input data, there is no cell whose value is strictly greater than the values of all neighboring cells.
In the third set of input data, the algorithm will select the cell and then terminate.

In the fourth set of input data, the algorithm will select the cell three times and then the cell twice.
