#K37207. Forest Conversion: Minimizing Adjacent Forests

    ID: 25925 Type: Default 1000ms 256MiB

Forest Conversion: Minimizing Adjacent Forests

Forest Conversion: Minimizing Adjacent Forests

You are given a grid with n rows and m columns. Each cell in the grid contains a character representing a type of terrain. In particular, 'F' represents a forest cell. Two cells are considered adjacent if they share a common side (up, down, left, or right).

Your task is to convert the minimum number of forest cells into water cells (represented by 'W') such that no two forest cells remain adjacent. When you convert a forest cell, it immediately changes to water, and this can affect subsequent checks in the grid scan.

Note: The grid is processed in row-major order (from top to bottom and left to right), and if a forest cell is found to be adjacent to any other forest cell, it is converted into water. The conversion is counted, and the cell is immediately updated, which may influence further decisions.

Input/Output Example:

Input:
4 5
WWLFW
LLFFF
MWLFW
WLFLF

Output: 3

</p>

inputFormat

The input is read from stdin and has the following format:

  • The first line contains two space-separated integers: n (the number of rows) and m (the number of columns).
  • This is followed by n lines, each containing a string of length m, representing a row of the grid.

outputFormat

Output a single integer representing the minimum number of conversions (from 'F' to 'W') required so that no two forest cells are adjacent.

## sample
4 5
WWLFW
LLFFF
MWLFW
WLFLF
3

</p>