


With every passing night, the virus spreads to all the adjacent cells, unless the cells are blocked by a wall. You need to build walls in order to stop the virus from spreading in the whole country. Note that you can only install walls around only one region which is the affected area that threatens to infect the most uninfected cells of the matrix in one day.
Given the state of each cell, your task is to find the number of walls you used to stop as many cells as possible from being infected.
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.
Output Format:
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.
Note:
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.
2
3 3
1 1 1
1 0 0
1 1 1
3 9
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
5
13
In the first test case, we can see in the above model, in order to save the uninfected cells from getting infected, we would require 5 walls.
In the second test case, we can see in the model, in order to save the uninfected cells from getting infected, we would require 13 walls.
.png)
2
2 2
1 0
0 0
3 3
1 1 1
1 0 1
1 1 1
2
4
Can you think of using Depth First Search to solve the problem?
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.
Algorithm:
O((N * M) ^ 4/3), where N and M are the dimensions of the model.
Since the size of infected region with respect to time ‘T’ is (T ^ 2) and after every passing time, the infected area decreases, so we can expand this expression as: T ^ 2 + (T - 1) ^ 2 + (T - 2) ^ 2 and so on…, which is equal to 1/6 * T(2*T ^ 2 + 3*T + 1), which means, for number of iterations required can be approximated as T ^ 3 <= N * M, which gives an upper bound for T = ((N * M) ^ ⅓) iterations and for every iteration it takes O(N * M) time. Hence, the overall time complexity of the above approach will be O((N * M) ^ 4/3).
O(N * M), where N and M are the dimensions of the model
The space required for the sets is O(N * M), so the overall complexity will be O(N * M).