Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $n \times m$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon).

The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.

In one move, you can take one icon and move it to any empty cell in the desktop.

Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $q$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?

Note that queries are permanent and change the state of the desktop.


-----Input-----

The first line of the input contains three integers $n$, $m$ and $q$ ($1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5$) — the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.

The next $n$ lines contain the description of the desktop. The $i$-th of them contains $m$ characters '.' and '*' — the description of the $i$-th row of the desktop.

The next $q$ lines describe queries. The $i$-th of them contains two integers $x_i$ and $y_i$ ($1 \le x_i \le n; 1 \le y_i \le m$) — the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).


-----Output-----

Print $q$ integers. The $i$-th of them should be the minimum number of moves required to make the desktop good after applying the first $i$ queries.


-----Examples-----

Input
4 4 8
..**
.*..
*...
...*
1 3
2 3
3 1
2 3
3 4
4 3
2 3
2 2
Output
3
4
4
3
4
5
5
5
Input
2 5 5
*...*
*****
1 3
2 2
1 3
1 5
2 3
Output
2
3
3
3
2


-----Note-----

None

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Data structures

Implementation

Greedy algorithms


Solutions:
import sys
input = sys.stdin.readline

def solve():
	(R, C, Q) = map(int, input().split())
	s = []
	for _ in range(R):
		s.append(list(input().strip()))
	t = []
	for i in range(C):
		for j in range(R):
			t.append(s[j][i])
	total = t.count('*')
	current = t[total:].count('*')
	for _ in range(Q):
		(x, y) = map(int, input().split())
		x -= 1
		y -= 1
		now = y * R + x
		if t[now] == '*':
			if now >= total:
				current -= 1
			t[now] = '.'
			total -= 1
			if t[total] == '*':
				current += 1
		else:
			if now >= total:
				current += 1
			t[now] = '*'
			if t[total] == '*':
				current -= 1
			total += 1
		print(current)
solve()


import sys
input = sys.stdin.readline
write = sys.stdout.write
(n, m, q) = list(map(int, input().split()))
data = []
cnt = 0
for i in range(n):
	data.append(list(input().strip()))
	cnt += data[-1].count('*')
temp = 0
match = 0
for j in range(m):
	if temp == cnt:
		break
	for i in range(n):
		if temp == cnt:
			break
		temp += 1
		if data[i][j] == '*':
			match += 1
for _ in range(q):
	(x, y) = list(map(int, input().split()))
	x -= 1
	y -= 1
	temp = y * n + x + 1
	if data[x][y] == '.':
		data[x][y] = '*'
		if temp <= cnt:
			match += 1
		cnt += 1
		if data[(cnt - 1) % n][(cnt - 1) // n] == '*':
			match += 1
	else:
		if data[(cnt - 1) % n][(cnt - 1) // n] == '*':
			match -= 1
		data[x][y] = '.'
		cnt -= 1
		if temp <= cnt:
			match -= 1
	write(str(cnt - match) + '\n')


out = ''
(n, m, q) = [int(x) for x in input().split()]
map_ = [[] for _ in range(m)]
count = 0
for _ in range(n):
	line = input()
	for i in range(m):
		x = line[i]
		if x == '*':
			map_[i].append(1)
			count += 1
		else:
			map_[i].append(0)
y = 0
x = 0
count2 = 0
while y * n + x < count:
	if map_[y][x] == 1:
		count2 += 1
	x += 1
	if x == n:
		y += 1
		x = 0
for _ in range(q):
	(x, y) = [int(x) - 1 for x in input().split()]
	map_[y][x] = 1 - map_[y][x]
	if map_[y][x] == 1:
		count += 1
		ye = (count - 1) // n
		xe = (count - 1) % n
		count2 += map_[ye][xe]
		if y * n + x < ye * n + xe:
			count2 += 1
	else:
		ye = (count - 1) // n
		xe = (count - 1) % n
		count2 -= map_[ye][xe]
		if y * n + x <= ye * n + xe:
			count2 -= 1
		count -= 1
	out += str(count - count2) + '\n'
print(out)


(n, m, q) = map(int, input().split(' '))
desktop = []
count_icons = 0
for _ in range(n):
	desktop.append([c for c in input()])
	count_icons += desktop[-1].count('*')
desktop = [desktop[row][col] for col in range(m) for row in range(n)]
count_correct = sum((1 for c in desktop[:count_icons] if c == '*'))
for _ in range(q):
	(x, y) = map(int, input().split(' '))
	i = (y - 1) * n + (x - 1)
	if desktop[i] == '*':
		if desktop[count_icons - 1] == '*':
			count_correct -= 1
		count_icons -= 1
		if i < count_icons:
			count_correct -= 1
		desktop[i] = '.'
		print(count_icons - count_correct)
	else:
		if i < count_icons:
			count_correct += 1
		count_icons += 1
		desktop[i] = '*'
		if desktop[count_icons - 1] == '*':
			count_correct += 1
		print(count_icons - count_correct)


(n, m, q) = [int(i) for i in input().split()]
temp = [input() for _ in range(n)]
desktop = []
for i in range(m):
	for j in range(n):
		desktop.append(temp[j][i])
total = desktop.count('*')
ans = desktop[:total].count('.')
for _ in range(q):
	(x, y) = [int(i) - 1 for i in input().split()]
	if desktop[y * n + x] == '*':
		ans -= desktop[total - 1] == '.'
		desktop[y * n + x] = '.'
		total -= 1
		ans += y * n + x < total
	else:
		ans += desktop[total] == '.'
		desktop[y * n + x] = '*'
		total += 1
		ans -= y * n + x < total
	print(ans)


(n, m, q) = map(int, input().split())
S = [input() for _ in range(n)]
cnt = 0
for s in S:
	cnt += s.count('*')
S = [list(s) for s in S]
In = 0
Out = 0
for x in range(n):
	for y in range(m):
		if S[x][y] == '*':
			if n * y + x >= cnt:
				Out += 1
for i in range(q):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	if S[x][y] == '*':
		S[x][y] = '.'
		cnt -= 1
		xx = cnt % n
		yy = cnt // n
		if S[xx][yy] == '*':
			Out += 1
		if n * y + x > cnt:
			Out -= 1
	else:
		xx = cnt % n
		yy = cnt // n
		if S[xx][yy] == '*':
			Out -= 1
		cnt += 1
		S[x][y] = '*'
		if n * y + x >= cnt:
			Out += 1
	print(Out)


(n, m, q) = map(int, input().split())
a = []
arr = []
for i in range(n):
	row = input()
	a.append(row)
for j in range(m):
	for i in range(n):
		arr.append(a[i][j])
tot = arr.count('*')
inside = arr[0:tot].count('*')
for h in range(q):
	(x, y) = map(int, input().split())
	index = (y - 1) * n + x - 1
	limit = tot - 1
	if arr[index] == '.':
		arr[index] = '*'
		tot += 1
		if arr[limit + 1] == '*':
			inside += 1
		if index <= limit:
			inside += 1
	else:
		tot -= 1
		if arr[limit] == '*':
			inside -= 1
		if index < limit:
			inside -= 1
		arr[index] = '.'
	print(tot - inside)


from re import L
(n, m, N) = map(int, input().split())
desk = []
icons = 0
for _ in range(n):
	line = list(map(lambda i: 1 if i == '*' else 0, input()))
	desk.append(line)
	icons += sum(line)
in_place = 0
if icons < n * m / 2:
	i = 0
	j = 0
	for k in range(icons):
		in_place += desk[i][j]
		i = i + 1
		if i == n:
			i = 0
			j += 1
else:
	in_place = icons
	i = n - 1
	j = m - 1
	for k in range(n * m - icons):
		in_place -= desk[i][j]
		i = i - 1
		if i == -1:
			i = n - 1
			j -= 1
for _ in range(N):
	(x, y) = map(int, input().split())
	if desk[x - 1][y - 1] == 0:
		icons += 1
		in_place += desk[icons % n - 1][(icons - 1) // n]
	else:
		in_place -= desk[icons % n - 1][(icons - 1) // n]
		icons -= 1
	if x + n * (y - 1) <= icons:
		in_place += 1 if desk[x - 1][y - 1] == 0 else -1
	desk[x - 1][y - 1] = 1 - desk[x - 1][y - 1]
	print(icons - in_place)


(n, m, q) = map(int, input().split())
z = []
a = []
c = 0
ans = 0
for i in range(n):
	z.append(list(input()))
for i in range(m):
	for j in range(n):
		if z[j][i] == '*':
			a.append(1)
			c += 1
		else:
			a.append(0)
ans = c
for i in range(c):
	if a[i] == 1:
		ans -= 1
for i in range(q):
	(x, y) = map(int, input().split())
	if a[n * (y - 1) + x - 1] == 1:
		a[n * (y - 1) + x - 1] = 0
		c -= 1
		if n * (y - 1) + x - 1 > c:
			ans -= 1
		if a[c] == 1:
			ans += 1
	elif a[n * (y - 1) + x - 1] == 0:
		a[n * (y - 1) + x - 1] = 1
		c += 1
		if n * (y - 1) + x - 1 >= c - 1:
			ans += 1
		if c:
			if a[c - 1] == 1:
				ans -= 1
	print(ans)


def main():
	(n, m, q) = map(int, input().split())
	s = set()
	table = [False] * (n * m)
	num = 0
	for i in range(n):
		s1 = input()
		for j in range(m):
			if s1[j] == '*':
				s.add((i, j))
				table[j * n + i] = True
				num += 1
	x = num // n
	x1 = num % n
	ans = 0
	for (a, b) in s:
		if b >= x and (b != x or a >= x1):
			ans += 1
	num -= 1
	for _ in range(q):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		index = b * n + a
		if table[index]:
			num -= 1
			if index == num + 1:
				pass
			else:
				if index > num:
					ans -= 1
				if table[num + 1]:
					ans += 1
		else:
			num += 1
			if index == num:
				pass
			else:
				if index > num:
					ans += 1
				if table[num]:
					ans -= 1
		table[index] = not table[index]
		print(ans)
main()


(n, m, t) = map(int, input().split())
mapa = []
count = 0
for i in range(n):
	k = input()
	mmmmmmm = []
	for j in range(m):
		mmmmmmm.append(k[j])
		if k[j] == '*':
			count += 1
	mapa.append(mmmmmmm)
ray = (count - 1) // n
row = (count - 1) % n
i = 0
j = 0
bad = count * 1
while i <= ray:
	if i < ray:
		while j < n:
			if mapa[j][i] == '*':
				bad -= 1
			j += 1
		i += 1
		j = 0
	else:
		while j <= row:
			if mapa[j][i] == '*':
				bad -= 1
			j += 1
		i += 1
i -= 1
j -= 1
for p in range(t):
	(xx, yy) = map(int, input().split())
	if mapa[xx - 1][yy - 1] == '.':
		mapa[xx - 1][yy - 1] = '*'
		if yy - 1 > i or (yy - 1 == i and xx - 1 > j):
			bad += 1
		if j < n - 1:
			j += 1
		else:
			j = 0
			i += 1
		if mapa[j][i] == '*':
			bad -= 1
	else:
		if mapa[j][i] == '*':
			bad += 1
		mapa[xx - 1][yy - 1] = '.'
		if yy - 1 > i or (yy - 1 == i and xx - 1 >= j):
			bad -= 1
		if j == 0:
			i -= 1
			j = n - 1
		else:
			j -= 1
	print(bad)


(n, m, q) = list(map(int, input().split()))
s = []
cnt = 0
for _ in range(n):
	s += [list(input())]
	for elem in s[-1]:
		if elem == '*':
			cnt += 1
ans = cnt
for i in range(cnt):
	if s[i % n][i // n] == '*':
		ans -= 1
for _ in range(q):
	(x, y) = list(map(int, input().split()))
	x -= 1
	y -= 1
	if s[x][y] == '*':
		cnt -= 1
		ans -= 1
		if s[cnt % n][cnt // n] == '*':
			ans += 1
		s[x][y] = '.'
		if cnt > x + y * n:
			ans += 1
	else:
		cnt += 1
		ans += 1
		s[x][y] = '*'
		if s[(cnt - 1) % n][(cnt - 1) // n] == '*':
			ans -= 1
		if cnt > 1 + x + y * n:
			ans -= 1
	print(ans)


import math
(n, m, q) = map(int, input().split())
l = []
c = 0
for i in range(n):
	l.append([])
	s = input()
	c += s.count('*')
	for j in s:
		l[-1].append(j)
y = int(math.ceil(c / n) - 1)
x = c - n * y - 1
c1 = 0
for i in range(y):
	for j in range(n):
		if l[j][i] == '*':
			c1 += 1
for i in range(x + 1):
	if l[i][y] == '*':
		c1 += 1
for i in range(q):
	(x1, y1) = map(int, input().split())
	x1 -= 1
	y1 -= 1
	if l[x1][y1] == '.':
		l[x1][y1] = '*'
		c += 1
		y = int(math.ceil(c / n) - 1)
		x = c - n * y - 1
		if l[x][y] == '*':
			c1 += 1
		if y1 < y:
			c1 += 1
		elif y1 == y:
			if x1 < x:
				c1 += 1
		print(c - c1)
	else:
		if l[x][y] == '*':
			c1 -= 1
		l[x1][y1] = '.'
		c -= 1
		y = int(math.ceil(c / n) - 1)
		x = c - n * y - 1
		if y1 < y:
			c1 -= 1
		elif y1 == y:
			if x1 <= x:
				c1 -= 1
		print(c - c1)


(n, m, q) = map(int, input().split())
totcnt = 0
gudcnt = 0
b = []
b.append(0)
a = [input() for i in range(n)]
for j in range(m):
	for i in range(n):
		b.append(bool(a[i][j] == '*'))
		if a[i][j] == '*':
			totcnt += 1
for i in range(totcnt + 1):
	gudcnt += b[i]
for i in range(q):
	(x, y) = map(int, input().split())
	ind = n * (y - 1) + x
	if b[ind]:
		if b[totcnt]:
			gudcnt -= 1
		totcnt -= 1
		if ind <= totcnt:
			gudcnt -= 1
		b[ind] = 0
	else:
		b[ind] = 1
		if ind <= totcnt:
			gudcnt += 1
		totcnt += 1
		if b[totcnt]:
			gudcnt += 1
	print(totcnt - gudcnt)