The difference between the versions is in the costs of operations. Solution for one version won't work for another!

Alice has a grid of size $n \times m$, initially all its cells are colored white. The cell on the intersection of $i$-th row and $j$-th column is denoted as $(i, j)$. Alice can do the following operations with this grid:

Choose any subrectangle containing cell $(1, 1)$, and flip the colors of all its cells. (Flipping means changing its color from white to black or from black to white).

This operation costs $1$ coin.

Choose any subrectangle containing cell $(n, 1)$, and flip the colors of all its cells.

This operation costs $2$ coins.

Choose any subrectangle containing cell $(1, m)$, and flip the colors of all its cells.

This operation costs $4$ coins.

Choose any subrectangle containing cell $(n, m)$, and flip the colors of all its cells.

This operation costs $3$ coins.

As a reminder, subrectangle is a set of all cells $(x, y)$ with $x_1 \le x \le x_2$, $y_1 \le y \le y_2$ for some $1 \le x_1 \le x_2 \le n$, $1 \le y_1 \le y_2 \le m$.

Alice wants to obtain her favorite coloring with these operations. What's the smallest number of coins that she would have to spend? It can be shown that it's always possible to transform the initial grid into any other.


-----Input-----

The first line of the input contains $2$ integers $n, m$ ($1 \le n, m \le 500$) — the dimensions of the grid.

The $i$-th of the next $n$ lines contains a string $s_i$ of length $m$, consisting of letters W and B. The $j$-th character of string $s_i$ is W if the cell $(i, j)$ is colored white in the favorite coloring of Alice, and B if it's colored black.


-----Output-----

Output the smallest number of coins Alice would have to spend to achieve her favorite coloring.


-----Examples-----

Input
3 3
WWW
WBB
WBB
Output
3
Input
10 15
WWWBBBWBBBBBWWW
BBBBWWWBBWWWBBB
BBBWWBWBBBWWWBB
BBWBWBBWWWBBWBW
BBBBWWWBBBWWWBB
BWBBWWBBBBBBWWW
WBWWBBBBWWBBBWW
WWBWWWWBBWWBWWW
BWBWWBWWWWWWBWB
BBBWBWBWBBBWWBW
Output
74


-----Note-----

In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells $(2, 2), (2, 3), (3, 2), (3, 3)$. This would cost $3$ coins.

Source: codeforces | Difficulty: VERY_HARD
Tags: Greedy algorithms

Constructive algorithms


Solutions:
(n, m) = input().strip().split()
n = int(n)
m = int(m)
a = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n):
	s = input().strip()
	for j in range(m):
		if s[j] == 'B':
			a[i + 1][j + 1] = 1
		if i > 0:
			a[i][j + 1] ^= a[i + 1][j + 1]
		if j > 0:
			a[i + 1][j] ^= a[i + 1][j + 1]
		if i > 0 and j > 0:
			a[i][j] ^= a[i + 1][j + 1]
res = 0
sub = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if a[i][j] == 1:
			res += 1
		if i < n and j < m and a[i][j] and a[i][m] and a[n][j] and a[n][m]:
			sub = 1
print(res - sub)


def get_a(i, j):
	if i >= 0 and i < n and (j >= 0) and (j < m):
		return a[i][j]
	else:
		return 0
(n, m) = map(int, input().split())
a = [[0] * m for _ in range(n)]
b = [[0] * m for _ in range(n)]
for i in range(n):
	t = list(str(input()))
	for j in range(m):
		a[i][j] = 1 if t[j] == 'B' else 0
for i in range(n):
	for j in range(m):
		b[i][j] = get_a(i, j) ^ get_a(i + 1, j) ^ get_a(i, j + 1) ^ get_a(i + 1, j + 1)
ans = 0
t = 0
for i in range(n):
	for j in range(m):
		if b[i][j] == 1:
			ans += 1
		if i <= n - 2 and j <= m - 2 and (b[i][j] == 1) and (b[i][m - 1] == 1) and (b[n - 1][j] == 1) and (b[n - 1][m - 1] == 1):
			t = 1
print(ans - t)