Tracks in the Snow

Time limit2sMemory limit1300 MB

Summary
Given a grid where each tracked cell shows the most recent animal (R or F), find the minimum number of animals that crossed from the top-left to the bottom-right.
Level

Hard8 of 10

Topics
Graph, Greedy, DFS, Implementation
Solved
No attempts yet

Problem

In a forest there is a rectangular meadow that was covered with fresh snow in the morning (left in the figure below).

Rabbits and foxes that live in the forest cross the meadow and leave tracks in the snow. Every animal enters at the upper-left corner and leaves at the lower-right corner. In between it may wander back and forth, even crossing its own tracks. At most one animal is on the meadow at any moment, and no animal enters more than once.

Model the meadow as a grid of square cells. In a single step an animal moves to an orthogonally adjacent cell (never diagonally, never skipping a cell). When an animal steps onto a cell, its track covers every earlier track in that cell, so only the most recent animal's track is visible there.

For example, a rabbit first crosses from the top-left to the bottom-right (middle in the figure). Afterwards a fox crosses, partly covering the rabbit's tracks (right in the figure):

........ RRR..... FFR.....
........ ..RRR... .FRRR...
........ ..R..... .FFFFF..
........ ..RRRR.R ..RRRFFR
........ .....RRR .....FFF

You are given the state of the meadow at some later time: for every cell you know whether a track is visible and, if so, whether it was left by a rabbit or by a fox (the right panel above). Determine the minimum number NN of animals that must have crossed the meadow to produce the given pattern of tracks.

Input

The first line contains two integers HH and WW — the height and width of the meadow. Each of the next HH lines contains exactly WW characters describing one row of the map: . is untouched snow, R is a cell whose topmost track belongs to a rabbit, and F is a cell whose topmost track belongs to a fox. At least one cell carries a track.

Output

Print a single integer: the minimum number N≥1N \ge 1 of animals that could have left the tracks shown in the input.

Constraints

  • 1≤H,W≤40001 \le H, W \le 4000

Examples3

  1. Example 1

    Input
    5 8
    FFR.....
    .FRRR...
    .FFFFF..
    ..RRRFFR
    .....FFF
    
    Expected output
    2
    
  2. Example 2

    Input
    2 2
    FF
    FF
    
    Expected output
    1
    
  3. Example 3

    Input
    3 3
    FFF
    FRF
    FFF
    
    Expected output
    2