



Let’s say you have a grid [[1,0],[1,1]]. We can say the computer ‘ARR[0][0]’ is a connected computer because there is a computer in its column other than itself. We can say the computer ‘arr[1][0]’ is a connected computer because there is a computer in its row and column other than itself. We can say the computer ‘arr[1][1]’ is a connected computer because there is a computer in its row other than itself. Therefore the number of connected computers is 3.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains two space-separated ‘N’ and ‘M’ representing the number of the rows and columns in ‘ARR’ respectively.
Each of the next ‘N’ lines of input contains ‘M’ single space-separated integers representing the elements of ‘ARR’.
For each test case, return the number of connected computers.
Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 100
1 <= M <= 100
‘ARR[i][j]’ = 0 or 1
Where ‘ARR[i][j]’ is an element of grid ‘ARR’.
Time Limit: 1 sec
In this approach, we will find if there are any other computers present in ‘i-th’ row or ‘j-th’ column for the ‘(i, j)-th’ computer.
The steps are as follows:
In this approach we simply optimize the approach-1, We will maintain two auxiliary arrays/lists ‘COLUMNCTR’ of size ‘M' and ‘ROWCTR’ of size ‘N’. ‘COLUMNCTR[j]’ stores the number of computers in ‘j-th’ column. ‘ROWCTR[i]’ stores the number of computers in the ‘i-th’ row. if ‘ARR[i][j]' ==1, then we don’t need to iterate column/row multiple times to check if there is a computer.
The steps are as follows: