#P1986B. Matrix Stabilization

    ID: 9335 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>brute forcedata structuresgreedysortings*1000

Matrix Stabilization

No submission language available for this problem.

Description

You are given a matrix of size n×mn \times m, where the rows are numbered from 11 to nn from top to bottom, and the columns are numbered from 11 to mm from left to right. The element at the intersection of the ii-th row and the jj-th column is denoted by aija_{ij}.

Consider the algorithm for stabilizing matrix aa:

  1. Find the cell (i,j)(i, j) 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 ii, and if there are still multiple cells, choose the one with the smallest value of jj.
  2. Set aij=aij1a_{ij} = a_{ij} - 1.
  3. Go to step 11.

In this problem, cells (a,b)(a, b) and (c,d)(c, d) are considered neighbors if they share a common side, i.e., ac+bd=1|a - c| + |b - d| = 1.

Your task is to output the matrix aa 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 tt (1t1041 \leq t \leq 10^4) — 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 nn and mm (1n,m100,nm>11 \leq n, m \leq 100, n \cdot m > 1) — the number of rows and columns of matrix aa.

The next nn lines describe the corresponding rows of the matrix. The ii-th line contains mm integers ai1,ai2,,aima_{i1}, a_{i2}, \ldots, a_{im} (1aij1091 \leq a_{ij} \leq 10^9).

It is guaranteed that the sum of nmn \cdot m over all sets of input data does not exceed 21052 \cdot 10^5.

For each set of input data, output nn lines with mm numbers in each line — the values of the cells of matrix aa after the stabilization algorithm.

Input

Each test consists of multiple sets of input data. The first line contains a single integer tt (1t1041 \leq t \leq 10^4) — 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 nn and mm (1n,m100,nm>11 \leq n, m \leq 100, n \cdot m > 1) — the number of rows and columns of matrix aa.

The next nn lines describe the corresponding rows of the matrix. The ii-th line contains mm integers ai1,ai2,,aima_{i1}, a_{i2}, \ldots, a_{im} (1aij1091 \leq a_{ij} \leq 10^9).

It is guaranteed that the sum of nmn \cdot m over all sets of input data does not exceed 21052 \cdot 10^5.

Output

For each set of input data, output nn lines with mm numbers in each line — the values of the cells of matrix aa after the stabilization algorithm.

Sample Input 1

6
1 2
3 1
2 1
1
1
2 2
1 2
3 4
2 3
7 4 5
1 8 10
5 4
92 74 31 74
74 92 17 7
31 17 92 3
74 7 3 92
7 31 1 1
3 3
1000000000 1 1000000000
1 1000000000 1
1000000000 1 1000000000

Sample Output 1

1 1 
1 
1 
1 2 
3 3 
4 4 5 
1 8 8 
74 74 31 31 
74 74 17 7 
31 17 17 3 
31 7 3 3 
7 7 1 1 
1 1 1 
1 1 1 
1 1 1

Note

In the first set of input data, the algorithm will select the cell (1,1)(1, 1) 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 (2,2)(2, 2) and then terminate.

In the fourth set of input data, the algorithm will select the cell (1,1)(1, 1) three times and then the cell (2,3)(2, 3) twice.