#K38247. Distinct Regions in a Maze

    ID: 26156 Type: Default 1000ms 256MiB

Distinct Regions in a Maze

Distinct Regions in a Maze

You are given a maze represented as an \(n \times m\) grid. Each cell of the grid is either an empty space denoted by . or a wall denoted by #. Two empty cells are considered connected if they are adjacent vertically or horizontally. In other words, two cells at positions \((r_1, c_1)\) and \((r_2, c_2)\) are connected if they satisfy the condition \(|r_1 - r_2| + |c_1 - c_2| = 1\).

Your task is to count the number of distinct regions of connected empty cells in the maze.

Example: For the following maze of size 5 by 6:

..##..
#..##.
#.##..
..####
#..#..

There are 3 distinct regions of connected empty spaces.

inputFormat

The input is read from standard input.

The first line contains two integers n and m (1 ≤ n, m ≤ 1000), representing the number of rows and columns of the maze respectively.

The following n lines each contain a string of m characters. Each character is either . (an empty cell) or # (a wall).

outputFormat

Output a single integer to standard output, representing the number of distinct regions of connected empty cells in the maze.

## sample
5 6
..##..
#..##.
#.##..
..####
#..#..
3