#C10462. Minimum Additional Flower Patches

    ID: 39670 Type: Default 1000ms 256MiB

Minimum Additional Flower Patches

Minimum Additional Flower Patches

Given an (n \times m) garden grid, each cell is either a flower patch ('F') or empty ('.'). Your task is to determine the minimum number of additional flower patches required to connect all existing flower patches into a single connected component. Two flower patches are connected if they are adjacent horizontally or vertically.

Formally, if there are (k) connected components of flower patches, you need to add (k - 1) patches to connect them. If there are no flowers or all flowers are already connected, the answer is 0.

inputFormat

The first line contains two integers, (n) and (m), representing the number of rows and columns of the grid. The next (n) lines each contain a string of length (m) consisting of characters 'F' and '.', representing the garden.

outputFormat

Print a single integer representing the minimum number of additional flower patches required.## sample

4 5
F..F.
.....
.F.F.
...F.
3