#C3267. Counting Forests

    ID: 46675 Type: Default 1000ms 256MiB

Counting Forests

Counting Forests

You are given a grid of size \( n \times m \) where each cell is either a tree ('T') or an empty cell ('.'). A forest is defined as a connected group of trees that are adjacent vertically or horizontally (not diagonally). Your task is to compute the number of distinct forests in the grid.

Example:

For the grid below:

.....
.TTT.
.T...
.....

There is only 1 forest.

The input is read from standard input and the result must be written to standard output.

inputFormat

The first line of the input contains two integers \( n \) and \( m \) (the number of rows and columns, respectively). The following \( n \) lines each contain a string of length \( m \) consisting only of the characters 'T' and '.'.

\( 1 \leq n, m \leq 1000 \)

outputFormat

Output a single integer representing the number of distinct forests in the grid.

## sample
4 5
.....
.TTT.
.T...
.....
1