The Village Boundaries
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'.
Example 1
3 4 FFBF FBFB BBFF
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
2 2 FF FF
0
All fertile cells, no barren neighbors. Count is 0.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
3 4 FFBF FBFB BBFF
2 2 FF FF