#K3086. Unreachable Buildings

    ID: 24880 Type: Default 1000ms 256MiB

Unreachable Buildings

Unreachable Buildings

You are given a grid representing a city. The grid has h rows and w columns, where each cell is one of the following characters:

  • B representing a building
  • F representing a fire station
  • S representing a street

A fire station sends out signals that can travel through street cells (S) in the four cardinal directions (up, down, left, right), but the signal stops when it hits a building. However, when the signal reaches a building, that building is considered reached. Your task is to determine the number of buildings that are unreachable from any fire station.

Formally, given a grid of size \(h \times w\) where \(h\) is the number of rows and \(w\) is the number of columns, count the number of cells containing B that cannot be reached from any cell containing F by moving only through cells containing S (the movement is allowed in four directions: up, down, left, right).

inputFormat

The input is read from standard input (stdin) with the following format:

h w
row_1
row_2
... 
row_h

Here, the first line contains two integers h and w, denoting the number of rows and columns. Each of the following h lines contains a string of length w representing a row in the grid.

outputFormat

Output a single integer to standard output (stdout), representing the number of buildings that cannot be reached by any fire station's signal.

## sample
4 5
BBSSB
BFFSB
BBSSB
BSSFB
2