Ninja is on a secret mission and wants to reach the enemy's base camp. However, his enemies have planted bombs in a grid of βNβ rows and βMβ columns called βBOMB-GRIDβ. To reach the camp, Ninja has to safely cross βBOMB-GRIDβ. The value at each cell in βBOMB-GRIDβ denotes the power of that bomb. An empty cell (without a bomb) is represented as 0. The βBOMB-GRIDβ has the following properties:
1. If three or more bombs with the same power are adjacent vertically or horizontally, then they blast at the same time immediately and their cells become empty.
2. After the bombs blast simultaneously if an empty cell on βBOMB-GRIDβ has a bomb on top of itself, then these bombs will drop until they hit a bomb or hit at the bottom at the same time. (No new bombs will drop from outside of the top boundary of the βBOMB-GRID').
3. After the above steps, there may exist more bombs that can be blasted. If so, the above steps will be repeated.
4. If there does not exist more bombs that can be blasted (ie. the 'BOMB-GRID' is safe), then return the current βBOMB-GRIDβ.
5. The time taken by bombs to shift from one position to another can be neglected.
We will call βBOMB-GRID' safe if and only if no more blasts happen in βBOMB-GRIDβ. Ninja wants to know the safe state of βBOMB-GRIDβ after every possible blast.
As Ninja is busy planning, he asks you for help. Can you help Ninja determine the safe state of βBOMB-GRIDβ?
For Example :For the βBOMB-GRIDβ shown below, the third grid represents a safe state i.e after all possible blasts.

The first line of input contains an integer βTβ denoting the number of test cases. Then each test case follows.
The first line of each test case contains two single space-separated integers βNβ and βMβ representing the number of rows and columns in βBOMB-GRIDβ, respectively.
The next βNβ lines of each test case contain βMβ single space-separated integers denoting the power of the bomb at each cell.
Output Format :
For each test case, print the safe state of βBOMB-GRIDβ in a row-wise manner.
Print the output of each test case in a separate line.
Note :
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 50
1 <= M <= 50
0 <= BOMB-GRID[i][j] <= 1000
Where BOMB-GRID[i][j] denotes the power of the bomb placed in the βiβ-th row and βjβ-th column of βBOMB-GRIDβ.
Time limit: 1 sec
2
4 3
1 0 1
2 0 4
2 1 1
1 2 3
3 3
1 1 1
1 2 1
1 1 1
1 0 1
2 0 4
2 1 1
1 2 3
0 0 0
0 0 0
0 2 0
For the first test case :
The given βBOMB-GRIDβ is safe as there exist no 3 or more than 3 bombs with the same powers adjacent together either horizontally or vertically.
For the second test case :
The given βBOMB-GRIDβ all the bombs with power 1 in row 0, row 2 and column 0 and column 2 (0-based indexing) will blast simultaneously and the bomb with power 2 will be shifted to the bottom of column 1 as shown in the output. The βBOMB-GRIDβ now becomes safe.
2
3 3
4 5 3
6 5 1
6 5 1
4 3
6 7 8
3 4 8
5 3 2
5 9 1
4 0 3
6 0 1
6 0 1
6 7 8
3 4 8
5 3 2
5 9 1
Try to use Brute Force with some optimizations
The idea behind this approach is to pick all the bombs which are adjacent to 2 or more bombs horizontally or vertically and then blast them at the same time. After that, shift all the remaining bombs to the bottom-most bomb or at the bottom of βBOMB-GRIDβ and replace the empty cells with 0s. Do this while we do not get a safe state of βBOMB-GRIDβ.
Algorithm
Do the following while we do not get a safe state of βBOMB-GRIDβ:
O((N*M)^2) where βNβ and βMβ are the number of rows and the columns in the BOMB-GRID
Because in the worst-case, we have to iterate βBOMB-GRIDβ after each blast and the maximum number of blasts can be βN*Mβ/3. So, the overall time complexity will be O((N * M)^2).
O(1)
We are not using any extra space so space complexity will be O(1).