A group of connected 1's forms an island. The task is to complete the method **findIslands()** which returns the **number of islands** present. The function takes three arguments the first is the boolean matrix **A** and then the next two arguments are **N** and **M** denoting the size(N*M) of the matrix A .

**Input:**

The first line of input will be the number of testcases **T**, then T test cases follow. The first line of each testcase contains two space separated integers N and M. Then in the next line are the NxM inputs of the matrix A separated by space .

**Output:**

For each testcase in a new line, print the number of islands present.

**User Task:**

Since this is a functional problem you don't have to worry about input, you just have to complete the function **findIslands().**

**Constraints:**

1 <= T <= 100

1 <= N, M <= 50

0 <= A[i][j] <= 1

**Example(To be used only for expected output) :
Input**

2

3 3

1 1 0 0 0 1 1 0 1

4 4

1 1 0 0 0 0 1 0 0 0 0 1 0 1 0 0

**Output**

2

2

**Explanation**:

**Testcase 1:** The graph will look like

1 1 0

0 0 1

1 0 1

Here, two islands will be formed

First island will be formed by elements {A[0][0] , A[0][1], A[1][2], A[2][2]}

Second island will be formed by {A[2][0]}**.**

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

tushki | 198 |

namansharmacse17 | 110 |

TusharMittal3 | 70 |

adkeaesph | 57 |

deepakchoudhary11 | 48 |

ARUN_PUNDIR | 720 |

manishgupta8497 | 623 |

AMAN__JAIN | 596 |

Abhimanyu_kumar | 562 |

Bicky_Shokeen | 532 |

blackshadows | 5335 |

Ibrahim Nash | 5223 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4584 |

Login to report an issue on this page.