You are given a 2D matrix ‘GRID’ of size ‘NxM’. If there is a city in a cell ‘(i, j)’, then ‘GRID[i][j] = 1’, otherwise it’s ‘0’. A state is a maximal connected group of cities. Two cities are connected if they are horizontally or vertically adjacent to each other.
The ‘GRID’ is said to be connected if there is exactly one state. Otherwise, it’s disconnected. You are allowed to remove any number of cities from the ‘GRID’. The task is to find and return the minimum number of cities that we need to remove to disconnect the ‘GRID’.
Example :‘N’ = 3, ‘M’ = 3
‘GRID’ =

We can disconnect the ‘GRID’ by removing the cities at locations ‘(0, 1)’ and ‘(0, 2)’. Thus, you should return ‘2’ as the answer.
Note :
1. A ‘GRID’ with zero cities is also disconnected.
2. You do not need to print anything; it has already been taken care of. Just implement the function.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
Each test case’s first line contains two integers, ‘N’ and ‘M’, denoting the number of rows and columns in ‘GRID’. Then, ‘N’ lines follow.
Each line contains an ‘M’ space-separated integers denoting each row’s values in ‘GRID’.
Output format :
For every test case, return the minimum number of cities to remove to disconnect the island.
1 <= T <= 10
1 <= N, M <= 100
Value in each element of ‘GRID’ = [0, 1]
Where ‘T’ is the number of test cases, ‘N’ is the number of rows in ‘GRID’, and ‘M’ is the number of columns in ‘GRID’.
Time limit: 1 second
2
3 4
1 0 1 1
1 1 1 1
1 0 1 1
3 4
1 1 1 1
1 0 0 1
1 1 1 1
1
2
Test Case 1:
We can disconnect the ‘GRID’ by removing the city at location ‘(1, 1)’.

Thus, you should return ‘1’ as the answer.
Test Case 2:
We can disconnect the ‘GRID’ by removing the cities at locations ‘(1, 0)’ and ‘(1, 3)’.

Thus, you should return ‘2’ as the answer.
2
2 1
1
1
4 4
1 1 0 0
1 1 0 0
0 0 1 1
0 0 1 1
2
0
If we consider the ‘GRID’ as an undirected graph, each cell’s degree is (<= 4). This shall reduce the number of possible answers in the result set drastically.
Observation: The answer is always less than equal to two (‘<= 2’).
Proof: For any state (with ‘>= 3’ cities), every city at its boundary is connected to ‘<= 3’ cities, and between those cities, there will be at least one city connected to exactly two other cities. This city (colored red) can be of one of the following four types:
If we remove the two grey-colored cities, then the city with red color will get disconnected from its state. Now, we know that the answer will never be greater than ‘2’.
To check if the ‘GRID’ is disconnected, we can do a Depth-first search (or Breadth-first search) traversal for any arbitrary city and mark all the reachable cities from it. If some cities remain unmarked, then the ‘GRID’ is disconnected.
We try to find if the ‘GRID’ can be disconnected by removing:
The ‘isDisconnected()’ function to check if the ‘GRID’ is disconnected:
The ‘dfs(integer x, integer y)’ function to mark all the cities connected to ‘(x, y)’:
Algorithm:
O((N*M)^2), where ‘N’ is the number of rows in ‘GRID’, and ‘M’ is the number of columns in ‘GRID’.
In the worst-case scenario, there are ‘O(N*M)’ cities. For each city, we check if the ‘GRID’ becomes disconnected after removing it in ‘O(N*M)’. Thus, the time complexity is ‘O((N*M)^2)’.
O(N*M), where ‘N’ is the number of rows in ‘GRID’, and ‘M’ is the number of columns in ‘GRID’.
The DFS stack size will be equal to the number of cities, i.e., ‘O(N*M)’. The ‘seen’ array also needs ‘O(N*M)’ space.