The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.

Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.

A feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.

To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.

Some examples of the most popular Haar features are given below.  [Image] 

Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.

A prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.

You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variable value.

You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.


-----Input-----

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.

Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.


-----Output-----

Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.


-----Examples-----
Input
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW

Output
2

Input
3 3
WBW
BWW
WWW

Output
4

Input
3 6
WWBBWW
WWBBWW
WWBBWW

Output
3

Input
4 4
BBBB
BBBB
BBBB
BBBW

Output
4



-----Note-----

The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:

  add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); $\left. \begin{array}{|r|r|r|r|r|r|r|r|} \hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\ \hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\ \hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\ \hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\ \hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\ \hline 1 & {1} & {1} & {1} & {1} & {1} & {1} & {1} \\ \hline \end{array} \right.$

 add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient  - 2 and variable value. [Image] 

Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 =  - 1, as required.

Source: codeforces | Difficulty: HARD
Tags: Implementation

Greedy algorithms


Solutions:
(n, m) = map(int, input().split(' '))
p = [input() for i in range(n)]
p1 = [[1 if p[i][j] == 'B' else -1 for j in range(m)] for i in range(n)]
tm = [0 for i in range(m)]
r = 0
for i in range(n - 1, -1, -1):
	for j in range(m - 1, -1, -1):
		if tm[j] != p1[i][j]:
			r = r + 1
			tp = p1[i][j] - tm[j]
			for l in range(j + 1):
				tm[l] = tm[l] + tp
print(r)


import sys

def solve():
	(n, m) = map(int, input().split())
	res = 0
	tab = [list(input()) for _ in range(n)]
	for row in range(n):
		for col in range(m):
			tab[row][col] = 1 if tab[row][col] == 'W' else -1
	for row in range(n - 1, -1, -1):
		for col in range(m - 1, -1, -1):
			if tab[row][col] != 0:
				diff = tab[row][col]
				res += 1
				for i in range(row + 1):
					for j in range(col + 1):
						tab[i][j] -= diff
	return res
if sys.hexversion == 50594544:
	sys.stdin = open('test.txt')
print(solve())


a = []
k = []
(n, m) = map(int, input().split())
for i in range(n + 1):
	if i < n:
		a.append([-1 if c == 'B' else 1 for c in input()])
	k.append([0 for j in range(m + 1)])
ans = 0
for i in range(n - 1, -1, -1):
	add = 0
	for j in range(m - 1, -1, -1):
		k[i][j] = add + k[i + 1][j]
		if k[i][j] != a[i][j]:
			ans += 1
			add += a[i][j] - k[i][j]
			k[i][j] = a[i][j]
print(ans)


(n, m) = map(int, input().split())
p = [input() for i in range(n)]
t = [0] * m
k = 0
for i in range(1, n + 1):
	s = 0
	for j in range(1, m + 1):
		s += t[-j]
		q = s + 2 * (p[-i][-j] == 'W') - 1
		t[-j] -= q
		s -= q
		if q:
			k += 1
print(k)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy
from itertools import chain, dropwhile, permutations, combinations
from collections import defaultdict, deque

def VI():
	return list(map(int, input().split()))

def LIST(n, m=None):
	return [0] * n if m is None else [[0] * m for i in range(n)]

def run2(n, m, f):
	c = LIST(n, m)
	ans = 0
	for i in range(n - 1, -1, -1):
		for j in range(m - 1, -1, -1):
			if f[i][j] != c[i][j]:
				ans += 1
				for k in range(i + 1):
					for l in range(j + 1):
						c[k][l] += f[i][j] - c[i][j]
	print(ans)

def main2(info=0):
	(n, m) = VI()
	f = list(range(n))
	for i in range(n):
		f[i] = [1 if x == 'W' else -1 for x in input()]
	run2(n, m, f)
main2()


import sys
input = []
input_index = 0

def next(type, number=None):

	def next():
		global input, input_index
		while input_index == len(input):
			if sys.stdin:
				input = sys.stdin.readline().split()
				input_index = 0
			else:
				raise Exception()
		input_index += 1
		return input[input_index - 1]
	if number is None:
		result = type(next())
	else:
		result = [type(next()) for _ in range(number)]
	return result
(n, m) = next(int, 2)
iis = [next(str) for _ in range(n)]
count = 0
vs = [0] * n
for j in range(m - 1, -1, -1):
	for i in range(n - 1, -1, -1):
		c = iis[i][j]
		if c == 'W' and vs[i] != 1:
			count += 1
			d = 1 - vs[i]
			for k in range(i + 1):
				vs[k] += d
		elif c == 'B' and vs[i] != -1:
			count += 1
			d = -1 - vs[i]
			for k in range(i + 1):
				vs[k] += d
print(count)


(n, m) = list(map(int, input().split()))
ss = [input() for i in range(n)]
s = []
add = []
for i in range(n):
	s.append([])
	add.append([])
	for j in range(m):
		s[i].append(1 if ss[i][j] == 'B' else -1)
		add[i].append(0)
rm = list(range(m - 1, -1, -1))
ans = 0
for i in range(n - 1, -1, -1):
	cadd = 0
	for j in rm:
		cadd += add[i][j]
		x = cadd + s[i][j]
		if x != 0:
			ans += 1
			cadd -= x
			for k in range(i):
				add[k][j] -= x
print(ans)


(n, m) = [int(x) for x in input().split()]
M = [[1 if k == 'B' else -1 for k in input()] for i in range(n)]
P = [[0 for j in range(m + 1)] for i in range(n + 1)]
for i in range(n):
	for j in range(m):
		P[i][j] += M[i][j]
		P[i + 1][j + 1] += M[i][j]
		P[i + 1][j] -= M[i][j]
		P[i][j + 1] -= M[i][j]
print(n * m - sum((P[i][1:].count(0) for i in range(1, n + 1))))


(n, m) = map(int, input().strip().split())
s = []
for i in range(n):
	s.append(input())
f = [[0 for i in range(m + 1)] for j in range(n + 1)]
ans = 0
for k in range(n + m, 1, -1):
	for i in range(n, 0, -1):
		j = k - i
		if j > m or j < 1:
			continue
		tmp = 0
		if s[i - 1][j - 1] == 'W':
			tmp = 1 - f[i][j]
		else:
			tmp = -1 - f[i][j]
		if tmp != 0:
			ans = ans + 1
			for x in range(1, i + 1):
				for y in range(1, j + 1):
					f[x][y] = f[x][y] + tmp
print(ans)


(n, m) = [int(i) for i in input().split()]
l = []
for i in range(n):
	l.append(input())
la = [0 for i in range(m)]
s = 0
for i in range(n - 1, -1, -1):
	cv = 0
	for j in range(m - 1, -1, -1):
		la[j] += cv
		if l[i][j] == 'W':
			if la[j] != 1:
				cv += 1 - la[j]
				la[j] = 1
				s += 1
		elif la[j] != -1:
			cv += -1 - la[j]
			la[j] = -1
			s += 1
print(s)