#K61042. Minimum Sprinklers for Fertile Land

    ID: 31221 Type: Default 1000ms 256MiB

Minimum Sprinklers for Fertile Land

Minimum Sprinklers for Fertile Land

You are given a grid of size \(M \times N\) where each cell is either fertile land (denoted by 'F') or barren land (denoted by 'B'). A sprinkler placed in any fertile land cell can water its entire connected component of fertile lands. Two cells are considered connected if they are adjacent horizontally or vertically. Your task is to determine the minimum number of sprinklers required to water all the fertile lands in the grid.

Note: Use depth-first search (DFS) or breadth-first search (BFS) to count the number of connected components of 'F' cells. Each connected component requires one sprinkler.

inputFormat

The first line of input contains two space-separated integers \(M\) and \(N\), representing the number of rows and columns respectively. This is followed by \(M\) lines, each containing a string of length \(N\) consisting of the characters 'F' or 'B'.

Example:

3 3
FFF
FBF
FFF

outputFormat

Output a single integer representing the minimum number of sprinklers required to cover all fertile lands.

Example:

1
## sample
3 3
FFF
FBF
FFF
1