#C5947. Counting Islands

    ID: 49652 Type: Default 1000ms 256MiB

Counting Islands

Counting Islands

Given a grid with n rows and m columns, where each cell is either a '.' or a '*', your task is to count the number of islands. An island is defined as a maximal group of adjacent '.' cells connected horizontally or vertically. Use an efficient algorithm such as DFS or BFS to traverse the grid.

For example, consider the grid below:

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

The number of islands in this grid is 4.

Note: There is no mathematical formula needed for this problem.

inputFormat

The input is read from stdin and is structured as follows:

  • The first line contains two integers: n and m, which represent the number of rows and columns of the grid respectively.
  • The following n lines each contain a string of length m composed solely of the characters '.' and '*'.

outputFormat

Print to stdout a single integer that denotes the number of islands found in the grid.

## sample
5 5
.*...
.*.*.
..*..
*.**.
.*..*
4