The package for this problem was not updated by the problem writer or Codeforces administration after we've upgraded the judging servers. To adjust the time limit constraint, a solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then the value 800 ms will be displayed and used to determine the verdict.

Codeforces Beta Round 37 |
---|

Finished |

Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests.
If you've seen these problems, a virtual contest is not for you - solve these problems in the archive.
If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive.
Never use someone else's code, read the tutorials or communicate with other person during a virtual contest.

graphs

greedy

shortest paths

*2600

No tag edit access

The problem statement has recently been changed. View the changes.

×
E. Trial for Chief

time limit per test

2 secondsmemory limit per test

256 megabytesinput

standard inputoutput

standard outputHaving unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen.

As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of *N* lines and *M* columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chief’s slab is painted. The one who paints a slab like that first becomes the new chief.

Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way.

Input

The first line contains two integers *N* and *M* (1 ≤ *N*, *M* ≤ 50) — the number of lines and columns on the slab. The next *N* lines contain *M* symbols each — the final coloration of the slab. *W* stands for the square that should be painted white and *B* — for the square that should be painted black.

Output

In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.

Examples

Input

3 3

WBW

BWB

WBW

Output

2

Input

2 3

BBB

BWB

Output

1

Codeforces (c) Copyright 2010-2023 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/01/2023 01:17:28 (g1).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|