Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You have been given a grid ‘ARR’ of size ‘N' * M’. ‘ARR[i][j]’ is ‘1’ if the computer is present at position ‘(i,j)’ otherwise it is zero. A computer is said to be a connected computer if there is a computer in its row or column other than itself. Your task is to return the number of connected computers.

```
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.
```

Detailed explanation

```
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
```

```
2
4 4
1 1 0 0
0 0 1 0
0 0 0 1
0 0 0 1
2 2
1 0
1 1
```

```
4
3
```

```
In test case 1, The computer ‘ARR[0][0]’ is a connected computer because there is a computer in its row other than itself. The computer ‘ARR[0][1]’ is a connected computer because there is a computer in its row other than itself. The computer ‘ARR[1][2]’ is not a connected computer because there is no computer in its row and column other than itself. The computer ‘ARR[3][2]’ is a connected computer because there is a computer in its column other than itself. The computer ‘ARR[3][3]’ is a connected computer because there is a computer in its column other than itself.
Therefore the answer is 4.
In test case 2, The computer ‘ARR[0][0]’ is a connected computer because there is a computer in its column other than itself. The computer ‘ARR[1][0]’ is a connected computer because there is a computer in its row and column other than itself. The computer ‘ARR[1][1]’ is a connected computer because there is a computer in its row other than itself.
Therefore the answer is 3.
```

```
2
2 2
1 0
0 1
2 2
1 1
1 1
```

```
0
4
```

```
In test case 1, The computer ‘ARR[0][0]’ is not a connected computer because there is no computer in its row and column other than itself. Also The computer ‘ARR[1][1]’ is not a connected computer because there is no computer in its row and column other than itself.
Therefore the answer is 0.
In test case 2, All the four computers are connected computers as each computer has other computer in its row and colum both.
Therefore the answer is 4.
```