#K39237. Count Safe Zones

    ID: 26376 Type: Default 1000ms 256MiB

Count Safe Zones

Count Safe Zones

In this problem, you are given a grid of size n×mn \times m. Each cell in the grid is either an empty cell represented by a dot ('.') or an obstacle represented by an asterisk ('*'). Two cells are considered connected if they are adjacent vertically or horizontally. Your goal is to determine the number of connected components (safe zones) consisting entirely of empty cells. This is a classic flood fill / DFS problem in grid graphs.

The problem can be formulated as follows:

Given an n×mn \times m grid, count the number of connected regions where each region is formed by adjacent cells (up, down, left, right) having a dot ('.') character.

For example, in the grid below:

.*..*
*..**
.*..*
***.*

There are 3 separate connected zones of empty cells.

Note: Use DFS or BFS to solve this problem effectively.

inputFormat

Input is read from standard input (stdin). The first line contains two integers nn and mm (1n,m10001 \leq n, m \leq 1000) representing the number of rows and columns. Each of the following nn lines contains a string of mm characters. Each character is either '.' (an empty cell) or '*' (an obstacle).

outputFormat

Output to standard output (stdout) a single integer representing the number of connected components (safe zones) of empty cells found in the grid.## sample

4 5
.*..*
*..**
.*..*
***.*
3