#K86757. Connected Components in a Grid
Connected Components in a Grid
Connected Components in a Grid
You are given a grid of size \(n \times m\) consisting of characters .
and #
. A cell containing .
represents an empty cell, while a cell containing #
represents a blocked cell.
Two empty cells are connected if they are adjacent horizontally or vertically. Your task is to compute the number of distinct connected components formed by the empty cells.
Example:
Input: 5 5 .#... .#.#. ###.. .#..# .#..#</p>Output: 3
Here, the grid has three connected components of empty cells.
Note: You can use either depth-first search (DFS) or breadth-first search (BFS) to solve the problem.
inputFormat
The first line contains two integers \(n\) and \(m\) (the number of rows and columns respectively). Each of the next \(n\) lines contains a string of length \(m\) composed only of the characters .
and #
.
\(1 \leq n, m \leq 10^3\)
outputFormat
Output a single integer representing the number of connected components of empty cells in the grid.
## sample5 5
.#...
.#.#.
###..
.#..#
.#..#
3