#C10433. Counting Islands
Counting Islands
Counting Islands
You are given a binary grid of size \(m \times n\) consisting of characters '0' and '1'. An island is defined as a group of one or more '1' cells that are connected horizontally or vertically (but not diagonally). Your task is to count the number of islands in the grid.
Note: Two cells are considered to be part of the same island if they are adjacent horizontally or vertically. You are required to read the input from stdin
and output the answer to stdout
.
For example, if the grid is:
4 5 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1
There are 3 islands in the grid.
inputFormat
The input consists of several lines:
- The first line contains two integers \(m\) and \(n\) representing the number of rows and the number of columns of the grid, respectively.
- The next \(m\) lines each contain \(n\) space-separated characters (either 0 or 1) representing the grid.
Read input from stdin
.
outputFormat
Output a single integer which is the number of islands found in the grid. Write the output to stdout
.
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
3
</p>