#C11337. Counting Islands
Counting Islands
Counting Islands
Given an (N \times M) binary matrix where each cell is either 0 (water) or 1 (land), your task is to determine the number of islands in the matrix. An island is defined as a group of one or more adjacent lands that are connected horizontally or vertically. The edges of the matrix are assumed to be surrounded by water.
For example, if the input matrix is:4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
Then the output should be 3
since there are three distinct islands.
inputFormat
The first line contains two space-separated integers (N) and (M), representing the number of rows and columns respectively. This is followed by (N) lines, each containing (M) space-separated integers (either 0 or 1) representing the rows of the matrix.
outputFormat
Output a single integer indicating the number of islands in the matrix.## sample
3 3
0 0 0
0 0 0
0 0 0
0
</p>