It may help you for using which concept to implement


#1

Using DFS helps you to solve this question and one similar question is the count the number of islands. you can see that question for any help.