


The first line contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first line of each test case contains two space-separated integers ‘N’ and ‘M’, denoting the dimensions of the country model.
Each of the next ‘N’ lines contains ‘M’ elements each denoting the state of the cell.
For each test case, print a single line containing a single integer denoting the number of walls required.
The output of each test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N, M <= 100
0 <= X <= 1
Where ‘T’ is the number of test cases, ‘N’ and ‘M’ denotes the dimensions of the model, ‘X’ denotes the element of the matrix.
Time limit: 1 sec.
The approach is to get clusters of infected areas in each step. Deal with the largest cluster and expand the rest of the clusters.
Let us understand this using an example:
INPUT:
1 1 1 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1
1 1 1 0 0 0 0 0 0
First iteration:
The highlighted area is the largest cluster as it infects the most cells. So, we will make walls around this area, in this case 11 walls and make the neighbors infected for the other infected area.
Second Iteration:
The highlighted area now is the largest cluster and the only cluster left in this case. So, make walls around the area, in this case 2 walls that touch the non-infected area.
Now, there are no more infected areas that can infect the cells. So, return the number of walls used, in this case 11 + 2 = 13 walls.