#C4267. Count Connected Components
Count Connected Components
Count Connected Components
You are given a grid of size \(n \times m\) containing characters '1' and '0'. A '1' represents a cell that is part of a component whereas a '0' represents water (or an empty cell). Two cells with '1' are connected if they are adjacent horizontally or vertically. Your task is to count the number of distinct connected components (groups) of '1's in the grid.
Input Format:
- The first line contains two integers \(n\) and \(m\), representing the number of rows and columns, respectively.
- The next \(n\) lines each contain \(m\) space-separated characters (each character is either '0' or '1').
Note: It is guaranteed that \(1 \le n, m \le 1000\).
inputFormat
The input is read from standard input. It has the following structure:
- The first line contains two integers \(n\) and \(m\) separated by space.
- The following \(n\) lines each contain \(m\) characters separated by spaces, representing the grid.
outputFormat
Output a single integer which is the number of connected components (groups) of '1's in the grid, printed to standard output.
## sample4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
3