cswork / OAOpen practice workspace

MicrosoftOA #23

Find Min Num Moves

Estimated MediumJudge-ready

Topics (AI-labelled)In-place ArraysGreedy

Each operation flips one B/W cell. Make every row and every column read the same forwards and backwards, and find the minimum number of flips. The input uses uppercase B/W throughout.

Input

This site's ranges: the first line contains N M (1..500), followed by N lines each with M characters B/W.

Output

Output a single integer, the answer.

Example 1

Input:

1 2
BW

Output:

1

Example 2

Input:

2 2
BW
WB

Output:

2

Example 3

Input:

3 3
BWB
WWW
BWB

Output:

0

Example notes

Example 1: the two cells must match, so flip either one; the answer is 1.

Example 2: the four corners belong to the same group, with two B and two W, so at least 2 cells must be flipped.

Example 3: every row and column is already a palindrome, so the answer is 0.

Editorial

Loading solution…