Coding Ninjas wants to test your concepts in 2D array. The task for you is: you are given a matrix of N rows and M columns with binary values (0, 1). You have to count number of islands in it. Islands are defined as conglomeration of connected 1's. Take for instance:
N=3, M=3 and binary matrix is as follows:
1 1 0
0 1 0
1 0 0
Here, in this example, we have two islands:
1. Formed by cells (1,1), (1,2), (2,2).
2. Formed by cells (3,1).
So, connected is defined as : all those 1's in the matrix which are reachable from a particular 1 are said to be connected to that particular 1. Since the term reachable is used, we have to define movements. The allowed movements from a given cell A[i][j] are:
1. Top (A[i-1][j])
2. Right (A[i][j+1])
3. Bottom (A[i+1][j])
4. Left (A[i][j-1])
You have to count and print number of islands in the given binary matrix.
Input Format:
The first line of input contains two space separated integers N (1 <= N <= 100)and M (1 <= M <= 100).
Each of the following N lines contain M space separated values, which lie in the range [0,1].
Constraints:
Time Limit: 1 second
Output format:
The first and only line of output contains count of number of islands in the given binary matrix.