QOJ.ac

QOJ

시간 제한: 1 s 메모리 제한: 128 MB

#23585. Grid Game

통계

Taeyang starts a game with $MN$ white and black stones placed on an $M\times N$ grid, as shown below.

problem_23585_ec8131d293e484e99357a6f8c0e3b23b.png

For any stone $X$, the $4$ stones immediately above, below, to the left, and to the right of $X$ are called its neighbors. Taeyang may choose one stone and change its color. Its neighbors of the same original color also change to the new color. This continues repeatedly: neighbors of stones whose colors change also change color if they have the same original color.

For example, changing stone $A$ in the picture above to white turns the black stones forming the shape “ㄷ” white, as shown below.

problem_23585_d0af010b6c22f49813a5605257b3100e.png

Next, changing stone $C$ to white produces the following picture.

problem_23585_74fc41bf0baf75fc892172fac72f2399.png

Then, changing stone $F$ to white and finally stone $E$ to white makes all stones white. Thus, all stones have become white after four color changes.

However, all stones can be made the same color with only two changes. First, changing stone $B$ to black produces the following picture.

problem_23585_e01e672992b8f344cec71a0526f0d1b5.png

Then, one can change stone $A$ to white to make all stones white, or change stone $D$ to black to make all stones black. Thus, two color changes suffice to make all stones the same color.

Given white and black stones on an $M\times N$ grid, write a program to find the minimum number of color changes needed to make all stones the same color.

Input

The first line contains two positive integers $M$ and $N$ describing the $M\times N$ grid, separated by a space. Both $M$ and $N$ are between $2$ and $100$, inclusive. Each of the following $M$ lines contains $N$ space-separated values describing one row of the grid: $0$ denotes a white stone and $1$ a black stone.

Output

Print on the first line the minimum number of color changes needed to make all stones the same color.

Examples

Input 1

5 6
1 1 0 1 0 0
1 0 0 1 1 1
1 1 0 1 1 1
0 0 0 0 0 0
1 1 1 0 1 1

Output 1

2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.