#K3086. Unreachable Buildings
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 buildingF
representing a fire stationS
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.
## sample4 5
BBSSB
BFFSB
BBSSB
BSSFB
2