The Village BoundariesEasy

The Village Boundaries

Easy+10 XP · +5 Auramatrixloopsconditional logic

The UptoSkills Arena hosts a farming simulation! Your task is to help determine the agricultural potential of a new plot of land represented by a grid. Some cells are fertile (marked 'F') and others are barren (marked 'B'). A farmer wants to know how many distinct fertile regions are adjacent (up, down, left, or right) to at least one barren cell. These are the plots bordering the 'wasteland', important for planning irrigation and pest control.

Given the land grid, count how many fertile cells have a barren neighbor.

Input

The first line contains two integers N and M (1 <= N, M <= 1000) representing the grid dimensions. The next N lines each contain M characters, either 'F' or 'B', describing the grid.

Output

A single integer: the count of fertile cells adjacent to at least one barren cell.

Constraints

1 <= N, M <= 1000 Grid contains only 'F' and 'B'.

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
3 4
FFBF
FBFB
BBFF
Output
6

Fertile cells at (0,0), (0,1), (1,0), (2,2), (2,3). Neighbors: (0,0) neighbors 'B' at (0,2); (0,1) neighbors 'B' at (0,2); (1,0) neighbors 'B' at (1,1) and (2,0); (2,2) neighbors 'B' at (1,2) and (2,1); (2,3) neighbors 'B' at (1,3) and (2,2). Total 5.

Example 2

Input
2 2
FF
FF
Output
0

All fertile cells, no barren neighbors. Count is 0.

Hints