


The first line contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first input line of each test case contains two single space-integers ‘N’ and ‘M’ representing the number of rows and columns of the grid, respectively.
From the second line of each test case, the next N lines represent the rows of the grid. Every row contains M single space-separated integers.
For each test case, the only line of output will print the number of islands in the grid.
Print the output of each test case in a separate line.
Note: You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 100
1 <= M <= 100
0 <= grid[i][j] <= 1
Time limit: 1 sec
The problem boils down to find the number of connected components in the grid.
If we are on a land cell and explore every cell connected to it 8-directionally (and recursively cells connected to those cells, and so on), then the total number of cells with land explored will be one island.
To ensure we don't count cells in the island more than once, we will mark 1(land) as 0 during the recursion call.
We have a function ‘numberOfIsland’ in which we are iterating over the whole grid.
We will initialise ‘islands’ to 0 to count the number of islands. Whenever we encounter a 1(land), we will call ‘dfs’ and increment ‘islands’ by 1.
The ‘dfs’ function works as follows:
We can also use breadth-first search technique to explore an island.
The ‘bfs’ function works as follows: