#C10189. Counting Regions in a Grid
Counting Regions in a Grid
Counting Regions in a Grid
You are given a rectangular grid consisting of cells. Each cell is either an empty cell, denoted by .
, or a rock cell, denoted by #
. Two empty cells are connected if they are adjacent vertically or horizontally. A region is defined as a maximal set of connected empty cells.
Your task is to count the number of distinct regions formed by empty cells in the grid. In mathematical terms, if we let \( R \) represent the number of regions, your goal is to compute:
[ R = \text{number of connected components of '.' in the grid} ]
The grid may have only one row or one column. Make sure your solution efficiently handles the recursive search without processing the same cell multiple times.
inputFormat
The first line of input contains two integers m and n separated by a space, representing the number of rows and columns respectively.
Each of the next m lines contains a string of length n made up solely of the characters .
and #
, denoting the grid.
outputFormat
Output a single integer representing the number of distinct regions of empty cells found in the grid.
## sample5 6
..#...
.#....
..##..
.####.
..#.#.
3