Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.

Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with m pixels in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy's move the pixel does not change, that is, it remains black. Pasha loses the game when a 2 × 2 square consisting of black pixels is formed. 

Pasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers i and j, denoting respectively the row and the column of the pixel to be colored on the current move.

Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2 × 2 square consisting of black pixels is formed.


-----Input-----

The first line of the input contains three integers n, m, k (1 ≤ n, m ≤ 1000, 1 ≤ k ≤ 10^5) — the number of rows, the number of columns and the number of moves that Pasha is going to perform. 

The next k lines contain Pasha's moves in the order he makes them. Each line contains two integers i and j (1 ≤ i ≤ n, 1 ≤ j ≤ m), representing the row number and column number of the pixel that was painted during a move.


-----Output-----

If Pasha loses, print the number of the move when the 2 × 2 square consisting of black pixels is formed.

If Pasha doesn't lose, that is, no 2 × 2 square consisting of black pixels is formed during the given k moves, print 0.


-----Examples-----
Input
2 2 4
1 1
1 2
2 1
2 2

Output
4

Input
2 3 6
2 3
2 2
1 3
2 2
1 2
1 1

Output
5

Input
5 3 7
2 3
1 2
1 1
4 1
3 1
5 3
3 2

Output
0

Source: codeforces | Difficulty: EASY
Tags: Complete search


Solutions:
from itertools import product

def sq(i, j):
	return ([i - 1, j - 1], [])
(n, m, k) = [int(v) for v in input().split()]
a = [] * k
black = [[0 for i in range(m + 2)] for j in range(n + 2)]
for q in range(k):
	a = [int(v) for v in input().split()]
	black[a[0]][a[1]] = 1
	(i, j) = a
	if sum([black[u][v] for (u, v) in product([i - 1, i], [j - 1, j])]) == 4 or sum([black[u][v] for (u, v) in product([i - 1, i], [j, j + 1])]) == 4 or sum([black[u][v] for (u, v) in product([i, i + 1], [j - 1, j])]) == 4 or (sum([black[u][v] for (u, v) in product([i, i + 1], [j, j + 1])]) == 4):
		print(q + 1)
		raise SystemExit
print(0)


import sys
import math
(n, m, k) = map(int, input().split())
field = []
for i in range(n + 2):
	field.append([])
	for j in range(m + 2):
		field[i].append(False)
for ind in range(k):
	(i, j) = map(int, input().split())
	field[i][j] = True
	for ii in (-1, 1):
		for jj in (-1, 1):
			if field[i + ii][j] and field[i][j + jj] and field[i + ii][j + jj]:
				print(ind + 1)
				return
print(0)


(n, m, k) = map(int, input().split())
flag = 0
d = set()
for i in range(k):
	(x, y) = map(int, input().split())
	d.add((x, y))
	for j in [-1, 0]:
		for k in [-1, 0]:
			if all(((x + j + a, y + k + b) in d for a in [0, 1] for b in [0, 1])):
				if not flag:
					print(i + 1)
				flag = 1
if not flag:
	print(0)


(n, m, k) = map(int, input().split())
A = []
for i in range(n):
	A.append([0] * m)
dic = [[1, 0], [1, -1], [0, -1], [-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0]]
C = []
T = 0
for item in range(k):
	C.append(list(map(int, input().split())))
for u in C:
	T += 1
	(i, j) = (u[0] - 1, u[1] - 1)
	A[i][j] = 1
	for d in range(3):
		if i + dic[d][0] >= n or i + dic[d][0] < 0 or j + dic[d][1] >= m or (j + dic[d][1] < 0):
			break
		elif A[i + dic[d][0]][j + dic[d][1]] == 0:
			break
	else:
		print(T)
		break
	for d in range(2, 5):
		if i + dic[d][0] >= n or i + dic[d][0] < 0 or j + dic[d][1] >= m or (j + dic[d][1] < 0):
			break
		elif A[i + dic[d][0]][j + dic[d][1]] == 0:
			break
	else:
		print(T)
		break
	for d in range(4, 7):
		if i + dic[d][0] >= n or i + dic[d][0] < 0 or j + dic[d][1] >= m or (j + dic[d][1] < 0):
			break
		elif A[i + dic[d][0]][j + dic[d][1]] == 0:
			break
	else:
		print(T)
		break
	for d in range(6, 9):
		if i + dic[d][0] >= n or i + dic[d][0] < 0 or j + dic[d][1] >= m or (j + dic[d][1] < 0):
			break
		elif A[i + dic[d][0]][j + dic[d][1]] == 0:
			break
	else:
		print(T)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
a = []
d = {}
for i in range(n + 1):
	a.append([])
	for j in range(m + 1):
		a[i].append(0)
		d[i, j] = 0

def check_lose(a, i, j):
	try:
		if a[i][j] == 1 and a[i + 1][j] == 1 and (a[i][j + 1] == 1) and (a[i + 1][j + 1] == 1):
			return True
	except:
		return False
	return False
got_ans = False
for i in range(1, k + 1):
	(x, y) = map(int, input().split())
	if got_ans:
		continue
	if d[x, y] > 0:
		continue
	d[x, y] = 1
	a[x][y] = 1
	if check_lose(a, x, y) or check_lose(a, x - 1, y) or check_lose(a, x, y - 1) or check_lose(a, x - 1, y - 1):
		print(i)
		got_ans = True
if not got_ans:
	print(0)


(n, m, k) = map(int, input().split())
board = set()
res = 0
for move in range(k):
	(i, j) = map(int, input().split())
	if (i - 1, j - 1) in board and (i, j - 1) in board and ((i - 1, j) in board) or ((i, j + 1) in board and (i + 1, j + 1) in board and ((i + 1, j) in board)) or ((i - 1, j) in board and (i - 1, j + 1) in board and ((i, j + 1) in board)) or ((i, j - 1) in board and (i + 1, j - 1) in board and ((i + 1, j) in board)):
		res = move + 1
		break
	board.add((i, j))
print(res)


(n, m, k) = map(int, input().split())
mx = [[0] * (m + 2) for i in range(n + 2)]

def check(i, j):
	if mx[i][j + 1] and mx[i + 1][j] and mx[i + 1][j + 1]:
		return True
	if mx[i][j + 1] and mx[i - 1][j] and mx[i - 1][j + 1]:
		return True
	if mx[i][j - 1] and mx[i - 1][j - 1] and mx[i - 1][j]:
		return True
	if mx[i][j - 1] and mx[i + 1][j - 1] and mx[i + 1][j]:
		return True
	return False
for i in range(k):
	(a, b) = map(int, input().split())
	mx[a][b] = 1
	if check(a, b):
		print(i + 1)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
f = [[0 for i in range(m)] for j in range(n)]
move = [list(map(int, input().split())) for i in range(k)]
ans = 0
for (i, mv) in enumerate(move):
	f[mv[0] - 1][mv[1] - 1] = 1
	if mv[0] != 1 and mv[1] != 1:
		if f[mv[0] - 2][mv[1] - 2] + f[mv[0] - 2][mv[1] - 1] + f[mv[0] - 1][mv[1] - 2] == 3:
			ans = i + 1
			break
	if mv[0] != n and mv[1] != 1:
		if f[mv[0]][mv[1] - 2] + f[mv[0] - 1][mv[1] - 2] + f[mv[0]][mv[1] - 1] == 3:
			ans = i + 1
			break
	if mv[0] != 1 and mv[1] != m:
		if f[mv[0] - 2][mv[1]] + f[mv[0] - 2][mv[1] - 1] + f[mv[0] - 1][mv[1]] == 3:
			ans = i + 1
			break
	if mv[0] != n and mv[1] != m:
		if f[mv[0]][mv[1]] + f[mv[0]][mv[1] - 1] + f[mv[0] - 1][mv[1]] == 3:
			ans = i + 1
			break
print(ans)


from sys import *

def inp(n):
	if n == 1:
		return map(int, stdin.readline().split())
	elif n == 2:
		return map(float, stdin.readline().split())
	else:
		return map(str, stdin.readline().split())

def check():
	for l in range(a - 1, a + 2, 2):
		for j in range(b - 1, b + 2, 2):
			if (l > 0 and l <= n) and (j > 0 and j <= m):
				arr = [[l, j], [a, b], [l, b], [a, j]]
				if len(list(filter(lambda x: mem[x[0]][x[1]], arr))) == 4:
					return True
	return False

def fill_2darr(r, c, x):
	return [[x for i in range(c)] for i in range(r)]
(n, m, k) = inp(1)
mem = fill_2darr(n + 1, m + 1, 0)
if n == 1 or m == 1:
	exit(print(0))
for i in range(1, k + 1):
	(a, b) = inp(1)
	mem[a][b] = 1
	if check():
		exit(print(i))
print(0)


(n, m, k) = map(int, input().split())
field = [[0] * (m + 2) for i in range(n + 2)]
k1 = range(k)
goij = [list(map(int, input().split())) for i in k1]
z = 0
z1 = 0
num = 0
for i in k1:
	kv = 1
	kv1 = 1
	kv2 = 1
	kv3 = 1
	z = goij[i][0]
	z1 = goij[i][1]
	field[z][z1] = 1
	if field[z - 1][z1 - 1] == 1:
		kv += 1
	if field[z - 1][z1] == 1:
		kv += 1
		kv1 += 1
	if field[z - 1][z1 + 1] == 1:
		kv1 += 1
	if field[z][z1 - 1] == 1:
		kv += 1
		kv2 += 1
	if field[z][z1 + 1] == 1:
		kv1 += 1
		kv3 += 1
	if field[z + 1][z1 - 1] == 1:
		kv2 += 1
	if field[z + 1][z1] == 1:
		kv2 += 1
		kv3 += 1
	if field[z + 1][z1 + 1] == 1:
		kv3 += 1
	if kv == 4 or kv2 == 4 or kv3 == 4 or (kv1 == 4):
		num = i + 1
		break
print(num)


(n, m, k) = map(int, input().split())
field = [[False for _ in range(m)] for _ in range(n)]
flag = False
for i in range(k):
	(x, y) = map(int, input().split())
	x -= 1
	y -= 1
	field[x][y] = True
	if 0 < x:
		if 0 < y:
			flag = field[x - 1][y - 1] and field[x - 1][y] and field[x][y - 1] or flag
		if y < m - 1:
			flag = field[x - 1][y + 1] and field[x - 1][y] and field[x][y + 1] or flag
	if x < n - 1:
		if 0 < y:
			flag = field[x + 1][y - 1] and field[x + 1][y] and field[x][y - 1] or flag
		if y < m - 1:
			flag = field[x + 1][y + 1] and field[x + 1][y] and field[x][y + 1] or flag
	if flag:
		print(i + 1)
		break
if not flag:
	print(0)


R = lambda : map(int, input().split())
(n, m, k) = R()
N = 1024
g = [[0] * N for _ in range(N)]

def T(r, c):
	return g[r][c] and g[r + 1][c] and g[r][c + 1] and g[r + 1][c + 1]
for i in range(k):
	(r, c) = R()
	g[r][c] = 1
	if T(r - 1, c - 1) or T(r - 1, c) or T(r, c - 1) or T(r, c):
		print(i + 1)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
s = [[0 for i in range(m + 2)] for j in range(n + 2)]
count = 0

def Lose(x, y):
	if s[x - 1][y] and s[x][y - 1] and s[x - 1][y - 1]:
		return True
	if s[x + 1][y] and s[x][y + 1] and s[x + 1][y + 1]:
		return True
	if s[x - 1][y] and s[x][y + 1] and s[x - 1][y + 1]:
		return True
	if s[x + 1][y] and s[x + 1][y - 1] and s[x][y - 1]:
		return True
	else:
		return False
for _ in range(k):
	(i, j) = map(int, input().split())
	count += 1
	s[i][j] = 1
	if Lose(i, j):
		print(count)
		break
else:
	print(0)


def has_rect(mat, step):
	mat[step[0]][step[1]] = True
	steps = ((0, 0), (0, 1), (1, 0), (1, 1))
	coefs = ((1, 1), (-1, 1), (1, -1), (-1, -1))
	for c in coefs:
		for s in steps:
			x = step[0] + c[0] * s[0]
			y = step[1] + c[1] * s[1]
			if 0 <= x < len(mat) and 0 <= y < len(mat[0]) and mat[x][y]:
				continue
			else:
				break
		else:
			return True
	return False
(n, m, k) = map(int, input().split())
mat = [[0 for x in range(m)] for x in range(n)]
for i in range(k):
	step = tuple(map(int, input().split()))
	step = (step[0] - 1, step[1] - 1)
	if has_rect(mat, step):
		print(i + 1)
		break
else:
	print(0)


def main():
	(n, m, k) = map(int, input().split())
	l = list((tuple(map(int, input().split())) for _ in range(k)))
	m += 1
	n += 1
	field = [False] * (m * n)
	patterns = ((1, m, m + 1), (-m, -m + 1, 1), (-1, -m - 1, -m), (-1, m - 1, m))
	for (i, (y, x)) in enumerate(l):
		yx = y * m + x - m - 1
		field[yx] = True
		if any((all((field[yx + _] for _ in pattern)) for pattern in patterns)):
			print(i + 1)
			return
	print(0)
main()


nmk = input()
(ns, ms, ks) = nmk.split(' ')
n = int(ns)
m = int(ms)
k = int(ks)
counter = 1
field = [[[0, 0] for i in range(m)] for j in range(n)]
res = 0
while counter <= k:
	xy = input()
	(xs, ys) = xy.split(' ')
	x = int(xs) - 1
	y = int(ys) - 1
	if field[x][y][0] == 0:
		field[x][y][1] = counter
	field[x][y][0] = 1
	counter += 1
for i in range(n - 1):
	for j in range(m - 1):
		sum = field[i + 1][j][0] + field[i][j + 1][0] + field[i + 1][j + 1][0] + field[i][j][0]
		if sum == 4:
			a = max(field[i + 1][j][1], field[i][j + 1][1], field[i + 1][j + 1][1], field[i][j][1])
			if res == 0:
				res = a
			elif a < res:
				res = a
print(res)


def createl(m):
	l = []
	for i in range(0, m):
		l.append(0)
	return l
line = input()
x = line.split(' ')
n = int(x[0])
m = int(x[1])
k = int(x[2])
l = []
lf = []
kk = 0
for j in range(0, n):
	l.append(createl(m))
for i in range(0, k):
	line = input()
	x = line.split(' ')
	a = int(x[0])
	b = int(x[1])
	a = a - 1
	b = b - 1
	l[a][b] = 'x'
	if a > 0 and b > 0:
		if l[a - 1][b] == 'x' and l[a - 1][b - 1] == 'x' and (l[a][b - 1] == 'x'):
			print(i + 1)
			kk += 1
			break
	if a < n - 1 and b > 0:
		if l[a + 1][b] == 'x' and l[a + 1][b - 1] == 'x' and (l[a][b - 1] == 'x'):
			print(i + 1)
			kk += 1
			break
	if a < n - 1 and b < m - 1:
		if l[a + 1][b] == 'x' and l[a + 1][b + 1] == 'x' and (l[a][b + 1] == 'x'):
			print(i + 1)
			kk += 1
			break
	if a > 0 and b < m - 1:
		if l[a - 1][b] == 'x' and l[a - 1][b + 1] == 'x' and (l[a][b + 1] == 'x'):
			print(i + 1)
			kk += 1
			break
if kk == 0:
	print(0)


(n, m, k) = map(int, input().split())
field = [[0] * 1024 for _ in range(1024)]
for i in range(1, k + 1):
	(r, c) = map(int, input().split())
	field[r][c] = 1
	for (y, x) in ((r - 1, c - 1), (r - 1, c), (r, c - 1), (r, c)):
		if all((field[y + dy][x + dx] for dy in (0, 1) for dx in (0, 1))):
			print(i)
			exit()
print(0)


def Main():
	string = input().split(' ')
	n = int(string[0])
	m = int(string[1])
	k = int(string[2])
	if k == '0' or n == '0' or m == '0':
		print('0')
		return
	mas = []
	for i in range(n):
		mas.append([])
		for j in range(m):
			mas[i].append(0)
	act = True
	l = 0
	while act and k:
		string = input().split(' ')
		i = int(string[0]) - 1
		j = int(string[1]) - 1
		mas[i][j] = 1
		k = k - 1
		l = l + 1
		act = check(i, j, n, m, mas)
	if act == True:
		print('0')
		return
	if act == False:
		while k:
			string = input()
			k = k - 1
		print(str(l))
		return

def check(i, j, n, m, mas):
	if i != 0:
		if j != 0:
			if mas[i - 1][j - 1] == 1:
				if mas[i - 1][j] == 1:
					if mas[i][j - 1] == 1:
						return False
	if i + 1 != n:
		if j + 1 != m:
			if mas[i + 1][j + 1] == 1:
				if mas[i][j + 1] == 1:
					if mas[i + 1][j] == 1:
						return False
	if i != 0:
		if j + 1 != m:
			if mas[i - 1][j + 1] == 1:
				if mas[i - 1][j] == 1:
					if mas[i][j + 1] == 1:
						return False
	if i + 1 != n:
		if j != 0:
			if mas[i + 1][j - 1] == 1:
				if mas[i][j - 1] == 1:
					if mas[i + 1][j] == 1:
						return False
	return True
Main()


def main():
	(n, m, k) = read()
	board = [[0] * m for _ in range(n)]
	for i in range(k):
		(x, y) = read()
		x -= 1
		y -= 1
		board[x][y] = 1
		if x > 0 and y > 0 and board[x - 1][y - 1] and board[x - 1][y] and board[x][y - 1]:
			return i + 1
		if x > 0 and y < m - 1 and board[x - 1][y + 1] and board[x - 1][y] and board[x][y + 1]:
			return i + 1
		if x < n - 1 and y > 0 and board[x + 1][y - 1] and board[x + 1][y] and board[x][y - 1]:
			return i + 1
		if x < n - 1 and y < m - 1 and board[x + 1][y + 1] and board[x + 1][y] and board[x][y + 1]:
			return i + 1
	return 0

def read(mode=2):
	inputs = input().strip()
	if mode == 0:
		return inputs
	if mode == 1:
		return inputs.split()
	if mode == 2:
		return list(map(int, inputs.split()))

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='')
write(main())


(n, m, p) = map(int, input().split())
k = 1
M = [[-1] * (m + 2) for i in range(n + 2)]
for i in range(p):
	(a, b) = map(int, input().split())
	M[a][b] = 1
	if M[a + 1][b] == 1 and M[a + 1][b + 1] == 1 and (M[a][b + 1] == 1):
		print(k)
		exit()
	if M[a + 1][b] == 1 and M[a + 1][b - 1] == 1 and (M[a][b - 1] == 1):
		print(k)
		exit()
	if M[a - 1][b] == 1 and M[a - 1][b + 1] == 1 and (M[a][b + 1] == 1):
		print(k)
		exit()
	if M[a - 1][b] == 1 and M[a - 1][b - 1] == 1 and (M[a][b - 1] == 1):
		print(k)
		exit()
	k = k + 1
print(0)


def confirm_matrix(pt, flag):
	is_square = False
	x_pt = pt[0] - 1
	y_pt = pt[1] - 1
	if x_pt > 0 and y_pt > 0:
		result = flag[y_pt][x_pt - 1] and flag[y_pt - 1][x_pt - 1] and flag[y_pt - 1][x_pt]
		is_square = result
	if is_square:
		return True
	if x_pt < len(flag[0]) - 1 and y_pt > 0:
		result = flag[y_pt - 1][x_pt] and flag[y_pt - 1][x_pt + 1] and flag[y_pt][x_pt + 1]
		is_square = result
	if is_square:
		return True
	if x_pt > 0 and y_pt < len(flag) - 1:
		result = flag[y_pt + 1][x_pt] and flag[y_pt + 1][x_pt - 1] and flag[y_pt][x_pt - 1]
		is_square = result
	if is_square:
		return True
	if x_pt < len(flag[0]) - 1 and y_pt < len(flag) - 1:
		result = flag[y_pt][x_pt + 1] and flag[y_pt + 1][x_pt + 1] and flag[y_pt + 1][x_pt]
		is_square = result
	if is_square:
		return True
input_str = input()
input_array = input_str.split(' ')
n = int(input_array[0])
m = int(input_array[1])
k = int(input_array[2])
pixels_flag = [[False] * n for row in range(m)]
pixels = []
f = False
index = 0
for i in range(k):
	input_str = input()
	input_array = input_str.split(' ')
	x = int(input_array[0])
	y = int(input_array[1])
	pixels_flag[y - 1][x - 1] = True
	if not f and confirm_matrix((x, y), pixels_flag):
		f = True
		index = i + 1
		continue
if not index > 0:
	print(0)
else:
	print(index)


def check(board, n, m, i, j):
	if i < n - 1 and j < m - 1 and board[i][j] and board[i][j + 1] and board[i + 1][j] and board[i + 1][j + 1]:
		return True
	if i < n - 1 and j > 0 and board[i][j] and board[i][j - 1] and board[i + 1][j] and board[i + 1][j - 1]:
		return True
	if i > 0 and j < m - 1 and board[i][j] and board[i][j + 1] and board[i - 1][j] and board[i - 1][j + 1]:
		return True
	if i > 0 and j > 0 and board[i][j] and board[i][j - 1] and board[i - 1][j] and board[i - 1][j - 1]:
		return True
	return False
(n, m, k) = [int(s) for s in input().split()]
board = [[False] * m for _ in range(n)]
ans = 0
for r in range(k):
	(i, j) = [int(s) - 1 for s in input().split()]
	board[i][j] = True
	if check(board, n, m, i, j):
		ans = r + 1
		break
print(ans)


def check2x2(mpx, i, j):
	sumNW = mpx[i - 1][j] + mpx[i - 1][j - 1] + mpx[i][j - 1]
	if sumNW == 3:
		return True
	sumSW = mpx[i][j - 1] + mpx[i + 1][j - 1] + mpx[i + 1][j]
	if sumSW == 3:
		return True
	sumSE = mpx[i + 1][j] + mpx[i + 1][j + 1] + mpx[i][j + 1]
	if sumSE == 3:
		return True
	sumNE = mpx[i][j + 1] + mpx[i - 1][j + 1] + mpx[i - 1][j]
	if sumNE == 3:
		return True
	return False
(n, m, k) = map(int, input().split())
mpx = [[0 for i in range(m + 2)] for j in range(n + 2)]
step = 1
while step <= k:
	(i, j) = map(int, input().split())
	mpx[i][j] = 1
	if check2x2(mpx, i, j):
		print(step)
		break
	step += 1
else:
	print(0)


import sys
nmk = list(map(int, sys.stdin.readline().split()))
n = nmk[0]
m = nmk[1]
k = nmk[2]
a = [[0 for i in range(m + 2)] for j in range(n + 2)]
rs = 0
for i in range(k):
	tt = list(map(int, sys.stdin.readline().split()))
	a[tt[0]][tt[1]] = 1
	if rs == 0:
		if a[tt[0]][tt[1] + 1] == 1:
			if a[tt[0] - 1][tt[1]] == 1 and a[tt[0] - 1][tt[1] + 1] == 1 or (a[tt[0] + 1][tt[1]] == 1 and a[tt[0] + 1][tt[1] + 1] == 1):
				rs = i + 1
		if a[tt[0]][tt[1] - 1] == 1:
			if a[tt[0] - 1][tt[1]] == 1 and a[tt[0] - 1][tt[1] - 1] == 1 or (a[tt[0] + 1][tt[1]] == 1 and a[tt[0] + 1][tt[1] - 1] == 1):
				rs = i + 1
print(rs)


def check(matrix, i, j):
	a1 = matrix[i - 1][j] + matrix[i - 1][j - 1] + matrix[i][j - 1]
	if a1 == 3:
		return True
	a2 = matrix[i - 1][j] + matrix[i - 1][j + 1] + matrix[i][j + 1]
	if a2 == 3:
		return True
	a3 = matrix[i + 1][j] + matrix[i + 1][j - 1] + matrix[i][j - 1]
	if a3 == 3:
		return True
	a4 = matrix[i + 1][j] + matrix[i + 1][j + 1] + matrix[i][j + 1]
	if a4 == 3:
		return True
	return False
(n, m, k) = map(int, input().split())
pixels = [[0] * (m + 2) for i in range(n + 2)]
temp = 0
for k in range(k):
	(x, y) = map(int, input().split())
	pixels[x][y] = 1
	if check(pixels, x, y):
		print(k + 1)
		temp = 1
		break
if temp == 0:
	print(0)


(n, m, k) = map(int, input().split())
answer = [[False for i in range(m)] for i in range(n)]
flag = False
for i in range(k):
	(x, y) = map(int, input().split())
	if x <= n - 1 and y <= m - 1:
		if answer[x][y]:
			if answer[x - 1][y] and answer[x][y - 1]:
				print(i + 1)
				flag = True
				break
	if x - 2 >= 0 and y - 2 >= 0:
		if answer[x - 2][y - 2]:
			if answer[x - 1][y - 2] and answer[x - 2][y - 1]:
				print(i + 1)
				flag = True
				break
	if x - 2 >= 0 and y <= m - 1:
		if answer[x - 2][y]:
			if answer[x - 1][y] and answer[x - 2][y - 1]:
				print(i + 1)
				flag = True
				break
	if x <= n - 1 and y >= 2:
		if answer[x][y - 2]:
			if answer[x][y - 1] and answer[x - 1][y - 2]:
				print(i + 1)
				flag = True
				break
	answer[x - 1][y - 1] = True
for j in range(i + 1, k):
	(x, y) = map(int, input().split())
if flag == False:
	print(0)


(n, m, k) = input().split(' ')
n = int(n)
m = int(m)
k = int(k)
moves = []
grid = []
count = 0
ind = 0
dead = False
for i in range(k):
	(a, b) = input().split(' ')
	moves.append([int(a), int(b)])
for i in range(n):
	grid.append([])
	for j in range(m):
		grid[i].append(0)
while not dead:
	try:
		move = moves[count]
	except IndexError:
		break
	else:
		move = moves[count]
	count += 1
	dr1 = False
	dr2 = False
	dr3 = False
	dr4 = False
	grid[move[0] - 1][move[1] - 1] = 1
	if move[0] >= 2 and move[1] <= m - 1:
		dr1 = True
	if move[0] >= 2 and move[1] >= 2:
		dr2 = True
	if move[0] <= n - 1 and move[1] >= 2:
		dr3 = True
	if move[0] <= n - 1 and move[1] <= m - 1:
		dr4 = True
	if dr1:
		if grid[move[0] - 2][move[1]] and grid[move[0] - 2][move[1] - 1] and grid[move[0] - 1][move[1]]:
			dead = True
	if dr2:
		if grid[move[0] - 2][move[1] - 2] and grid[move[0] - 2][move[1] - 1] and grid[move[0] - 1][move[1] - 2]:
			dead = True
	if dr3:
		if grid[move[0]][move[1] - 2] and grid[move[0]][move[1] - 1] and grid[move[0] - 1][move[1] - 2]:
			dead = True
	if dr4:
		if grid[move[0]][move[1]] and grid[move[0] - 1][move[1]] and grid[move[0]][move[1] - 1]:
			dead = True
if dead:
	print(count)
else:
	print(0)


(n, m, k) = map(int, input().split())
mx = [[0] * (m + 2) for x in range(n + 2)]

def square_cheak(y, x):
	if mx[y][x + 1] and mx[y + 1][x + 1] and mx[y + 1][x]:
		return True
	if mx[y][x - 1] and mx[y + 1][x] and mx[y + 1][x - 1]:
		return True
	if mx[y - 1][x - 1] and mx[y - 1][x] and mx[y][x - 1]:
		return True
	if mx[y][x + 1] and mx[y - 1][x + 1] and mx[y - 1][x]:
		return True
	return False
for i in range(k):
	(y, x) = map(int, input().split())
	mx[y][x] = 1
	if square_cheak(y, x):
		print(i + 1)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
b = [[0 for x in range(m + 2)] for y in range(n + 2)]

def makes_square(i, j):
	if b[i - 1][j - 1] + b[i - 1][j] + b[i][j - 1] == 3:
		return 1
	if b[i][j - 1] + b[i + 1][j] + b[i + 1][j - 1] == 3:
		return 1
	if b[i + 1][j] + b[i + 1][j + 1] + b[i][j + 1] == 3:
		return 1
	if b[i - 1][j] + b[i - 1][j + 1] + b[i][j + 1] == 3:
		return 1
	return 0
loss = 0
for z in range(k):
	(i, j) = map(int, input().split())
	b[i][j] = 1
	if makes_square(i, j) == 1:
		loss = z + 1
		break
print(loss)


(n, m, k) = map(int, input().split())
matrix = [[0] * (m + 1) for i in range(n + 1)]
f = 0
mov = 0
for i in range(k):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	matrix[a][b] = 1
	if matrix[a][b] == matrix[a - 1][b] == matrix[a][b - 1] == matrix[a - 1][b - 1] and mov == 0:
		mov = i + 1
	elif matrix[a][b] == matrix[a + 1][b] == matrix[a][b + 1] == matrix[a + 1][b + 1] and mov == 0:
		f = 1
		mov = i + 1
	elif matrix[a][b] == matrix[a + 1][b] == matrix[a][b - 1] == matrix[a + 1][b - 1] and mov == 0:
		mov = i + 1
	elif matrix[a][b] == matrix[a - 1][b] == matrix[a][b + 1] == matrix[a - 1][b + 1] and mov == 0:
		mov = i + 1
print(mov)


(n, m, k) = [int(x) for x in input().split()]
l = [[0 for i in range(m + 2)] for i in range(n + 2)]
no = 0
for i in range(1, k + 1):
	(a, b) = [int(x) for x in input().split()]
	l[a][b] = 1
	if l[a][b] + l[a + 1][b] + l[a][b + 1] + l[a + 1][b + 1] == 4 or l[a - 1][b - 1] + l[a - 1][b] + l[a][b - 1] + l[a][b] == 4 or l[a][b] + l[a][b - 1] + l[a + 1][b] + l[a + 1][b - 1] == 4 or (l[a - 1][b] + l[a][b] + l[a - 1][b + 1] + l[a][b + 1] == 4):
		print(i)
		no = 1
		break
if no == 0:
	print(0)


(n, m, k) = [int(i) for i in input().split()]
mark = [[0 for i in range(m + 2)] for j in range(n + 2)]
for c in range(k):
	(i, j) = [int(i) for i in input().split()]
	mark[i][j] = 1
	if mark[i - 1][j - 1] == 1 and mark[i][j - 1] == 1 and (mark[i - 1][j] == 1) or (mark[i][j - 1] == 1 and mark[i + 1][j] == 1 and (mark[i + 1][j - 1] == 1)) or (mark[i][j + 1] == 1 and mark[i - 1][j] == 1 and (mark[i - 1][j + 1] == 1)) or (mark[i][j + 1] == 1 and mark[i + 1][j] == 1 and (mark[i + 1][j + 1] == 1)):
		print(c + 1)
		break
else:
	print(0)


def sum2(board, x, y, dx, dy):
	s = sum([board[x][y], board[x][y + dy], board[x + dx][y], board[x + dx][y + dy]])
	return s
(rows, cols, moves) = map(int, input().split())
if rows == 1 or cols == 1:
	print(0)
else:
	board = [[1 for col in range(cols)] for row in range(rows)]
	for move in range(moves):
		(row, col) = map(lambda x: int(x) - 1, input().split())
		board[row][col] = 0
		ok = True
		if row < rows - 1:
			if col < cols - 1 and sum2(board, row, col, 1, 1) == 0:
				ok = False
			if col > 0 and sum2(board, row, col, 1, -1) == 0:
				ok = False
		if row > 0:
			if col < cols - 1 and sum2(board, row, col, -1, 1) == 0:
				ok = False
			if col > 0 and sum2(board, row, col, -1, -1) == 0:
				ok = False
		if not ok:
			print(move + 1)
			break
	else:
		print(0)


(n, m, k) = map(int, input().split())
l = [[0] * (m + 2) for s in range(n + 2)]
for r in range(k):
	(i, j) = map(int, input().split())
	if l[i][j] == 0:
		l[i][j] = 1
	if l[i - 1][j - 1] == 1 and l[i][j - 1] == 1 and (l[i - 1][j] == 1):
		print(r + 1)
		break
	elif l[i - 1][j] == 1 and l[i - 1][j + 1] == 1 and (l[i][j + 1] == 1):
		print(r + 1)
		break
	elif l[i][j - 1] == 1 and l[i + 1][j - 1] == 1 and (l[i + 1][j] == 1):
		print(r + 1)
		break
	elif l[i][j + 1] == 1 and l[i + 1][j] == 1 and (l[i + 1][j + 1] == 1):
		print(r + 1)
		break
else:
	print(0)


(n, m, k) = input().split()
n = int(n)
m = int(m)
k = int(k)
l = []
for i in range(n):
	l1 = []
	for j in range(m):
		l1.append(0)
	l.append(l1)
flag = 0

def check(s1, s2, n, m):
	q = 0
	if s1 > 1 and s2 > 1:
		q += l[s1 - 1 - 1][s2 - 1]
		q += l[s1 - 1 - 1][s2 - 1 - 1]
		q += l[s1 - 1][s2 - 1 - 1]
		if q == 3:
			return 1
	q = 0
	if s1 < n and s2 > 1:
		q += l[s1 - 1][s2 - 1 - 1]
		q += l[s1 - 1 + 1][s2 - 1 - 1]
		q += l[s1 - 1 + 1][s2 - 1]
		if q == 3:
			return 1
	q = 0
	if s1 < n and s2 < m:
		q += l[s1 - 1 + 1][s2 - 1]
		q += l[s1 - 1 + 1][s2 - 1 + 1]
		q += l[s1 - 1][s2 - 1 + 1]
		if q == 3:
			return 1
	q = 0
	if s1 > 1 and s2 < m:
		q += l[s1 - 1][s2 - 1 + 1]
		q += l[s1 - 1 - 1][s2 - 1 + 1]
		q += l[s1 - 1 - 1][s2 - 1]
		if q == 3:
			return 1
	return 0
for i in range(k):
	(s1, s2) = input().split()
	(s1, s2) = (int(s1), int(s2))
	l[s1 - 1][s2 - 1] = 1
	if check(s1, s2, n, m):
		flag = 1
		break
if flag:
	print(i + 1)
elif check(s1, s2, n, m):
	print(k)
else:
	print(0)


[n, m, k] = list(map(int, input().split()))
mat = [[0 for i in range(m)] for j in range(n)]

def fbyf(mat, ilo, ihi, jlo, jhi):
	for i in range(ilo, ihi - 1):
		for j in range(jlo, jhi - 1):
			if mat[i][j] == 1 and mat[i + 1][j] == 1 and (mat[i][j + 1] == 1) and (mat[i + 1][j + 1] == 1):
				return True
	return False
count = 0
found = False
while k > 0:
	[i, j] = list(map(int, input().split()))
	mat[i - 1][j - 1] = 1
	count += 1
	ilo = max(0, i - 2)
	ihi = min(n, i + 1)
	jlo = max(0, j - 2)
	jhi = min(m, j + 1)
	if fbyf(mat, ilo, ihi, jlo, jhi):
		found = True
		break
	k -= 1
if found:
	print(count)
else:
	print(0)


def isfull(a, b, c, d):
	return a + b + c + d == 4

def check(n, m, k):
	store = [[False for x in range(m + 2)] for x in range(n + 2)]
	for i in range(0, k):
		t = input().split()
		r = int(t[0])
		c = int(t[1])
		store[r][c] = True
		if isfull(store[r][c], store[r + 1][c], store[r][c + 1], store[r + 1][c + 1]):
			return i + 1
		if isfull(store[r][c], store[r - 1][c], store[r][c - 1], store[r - 1][c - 1]):
			return i + 1
		if isfull(store[r][c], store[r - 1][c], store[r][c + 1], store[r - 1][c + 1]):
			return i + 1
		if isfull(store[r][c], store[r + 1][c], store[r][c - 1], store[r + 1][c - 1]):
			return i + 1
	return 0

def main():
	t = input().split()
	n = int(t[0])
	m = int(t[1])
	k = int(t[2])
	print(check(n, m, k))
main()


(n, m, k) = [int(x) for x in input().split()]
list = [[0 for x in range(m + 2)] for y in range(n + 2)]
h = 0
for i in range(k):
	(x, y) = [int(x) for x in input().split()]
	list[x - 1][y - 1] = 1
	if list[x - 2][y - 1] == list[x - 2][y - 2] == list[x - 1][y - 2] == 1 or list[x][y] == list[x][y - 1] == list[x - 1][y] == 1 or list[x - 1][y] == list[x - 2][y - 1] == list[x - 2][y] == 1 or (list[x][y - 1] == list[x - 1][y - 2] == list[x][y - 2] == 1):
		h = i + 1
		print(h)
		break
else:
	print(0)


(n, m, k) = map(int, input().split(' '))
field = [[0 for y in range(m + 1)] for x in range(n + 1)]
for step in range(k):
	(i, j) = map(int, input().split(' '))
	i -= 1
	j -= 1
	field[i][j] = 1
	if i > 0 and j > 0 and (field[i][j - 1] == 1) and (field[i - 1][j] == 1) and (field[i - 1][j - 1] == 1):
		print(step + 1)
		break
	elif i < n - 1 and j < m - 1 and (field[i][j + 1] == 1) and (field[i + 1][j] == 1) and (field[i + 1][j + 1] == 1):
		print(step + 1)
		break
	elif i > 0 and j < m - 1 and (field[i - 1][j] == 1) and (field[i][j + 1] == 1) and (field[i - 1][j + 1] == 1):
		print(step + 1)
		break
	elif i < n - 1 and j > 0 and (field[i][j - 1] == 1) and (field[i + 1][j] == 1) and (field[i + 1][j - 1] == 1):
		print(step + 1)
		break
else:
	print('0')


(n, m, k) = map(int, input().split())
d_x = [0, 0, -1, -1]
d_y = [0, -1, 0, -1]
a_p = [[0 for _ in range(m + 1)] for _ in range(n + 1)]
for i in range(k):
	(y_o, x_o) = map(int, input().split())
	a_p[y_o][x_o] = 1
	for j in range(4):
		y = y_o + d_y[j]
		x = x_o + d_x[j]
		if (x > 0 and y > 0) and (y + 1 <= n and x + 1 <= m) and (a_p[y][x] + a_p[y][x + 1] + a_p[y + 1][x] + a_p[y + 1][x + 1] == 4):
			print(i + 1)
			quit()
print(0)


(n, m, k) = map(int, input().split(' '))
M = [[0 for j in range(m - 1)] for i in range(n - 1)]
C = [[False for j in range(m)] for i in range(n)]
ans = 0
ansCnt = False
for w in range(k):
	(x, y) = map(int, input().split(' '))
	x -= 1
	y -= 1
	if (C[x][y] == False) & (ansCnt == False):
		C[x][y] = True
		for i in [0, -1]:
			for j in [0, -1]:
				if (x + i > -1) & (y + j > -1) & (x + i < n - 1) & (y + j < m - 1):
					M[x + i][y + j] += 1
					if M[x + i][y + j] == 4:
						ans = w + 1
						ansCnt = True
print(ans)


(n, m, k) = map(int, input().split())
board = [[False for _ in range(m + 2)] for _ in range(n + 2)]
sq = [[(-1, -1), (-1, 0), (0, -1)], [(0, -1), (1, -1), (1, 0)], [(0, 1), (1, 1), (1, 0)], [(-1, 1), (-1, 0), (0, 1)]]
for m in range(k):
	(i, j) = map(int, input().split())
	board[i][j] = True
	for (a, b, c) in sq:
		if board[i + a[0]][j + a[1]] and board[i + b[0]][j + b[1]] and board[i + c[0]][j + c[1]]:
			print(m + 1)
			exit()
print(0)


x = input()
(n, m, k) = x.split(maxsplit=100)
(n, m, k) = (int(n), int(m), int(k))
mapp = [[0 for j in range(m)] for i in range(n)]
maqq = [[0 for j in range(m - 1)] for i in range(n - 1)]
u = [(0, 0), (0, -1), (-1, 0), (-1, -1)]
key = False
for i in range(k):
	x = input()
	if key:
		continue
	(x, y) = x.split(maxsplit=100)
	(x, y) = (int(x) - 1, int(y) - 1)
	if mapp[x][y] == 1:
		continue
	else:
		mapp[x][y] += 1
		for j in range(4):
			try:
				assert x + u[j][0] >= 0 and y + u[j][1] >= 0
				maqq[x + u[j][0]][y + u[j][1]] += 1
				if maqq[x + u[j][0]][y + u[j][1]] == 4:
					print(i + 1)
					key = 1
					break
			except Exception:
				pass
if not key:
	print(0)


(n, m, k) = map(int, input().split())
ar = []
tr = [[1, 1], [-1, 1], [-1, -1], [1, -1]]
for x in range(n):
	ar.append([0] * m)
for v in range(k):
	(c, d) = map(int, input().split())
	c += -1
	d += -1
	ar[c][d] = 1
	for x in tr:
		if c + x[0] < n and c + x[0] >= 0 and (d + x[1] < m) and (d + x[1] >= 0):
			if ar[c][d + x[1]] == 1 and ar[c + x[0]][d] == 1 and (ar[c + x[0]][d + x[1]] == 1):
				print(v + 1)
				exit(0)
print(0)


(n, m, k) = [int(i) for i in input().split()]
mass = []
for i in range(n):
	mass.append([1] * m)
for i in range(k):
	(a, b) = [int(j) for j in input().split()]
	a -= 1
	b -= 1
	mass[a][b] = 0
	if a > 0 and b > 0 and (mass[a - 1][b] == 0) and (mass[a - 1][b - 1] == 0) and (mass[a][b - 1] == 0) or (a > 0 and b < m - 1 and (mass[a - 1][b] == 0) and (mass[a - 1][b + 1] == 0) and (mass[a][b + 1] == 0)) or (a < n - 1 and b < m - 1 and (mass[a][b + 1] == 0) and (mass[a + 1][b + 1] == 0) and (mass[a + 1][b] == 0)) or (a < n - 1 and b > 0 and (mass[a][b - 1] == 0) and (mass[a + 1][b - 1] == 0) and (mass[a + 1][b] == 0)):
		print(i + 1)
		break
else:
	print(0)


def q508a():
	(n, m, k) = [int(x) for x in input().split()]
	arr = [0] * n * m
	for move_num in range(k):
		(i, j) = [int(x) for x in input().split()]
		arr[(i - 1) * m + (j - 1)] = 1
		if check_grid(arr, i, j, n, m):
			print(move_num + 1)
			exit()
	print(0)

def check_grid(arr, i, j, n, m):
	i = i - 1
	j = j - 1
	if j != 0 and i != 0 and (arr[(i - 1) * m + j - 1] == 1) and (arr[i * m + j - 1] == 1) and (arr[(i - 1) * m + j] == 1) and (arr[i * m + j] == 1):
		return True
	elif j != m - 1 and i != 0 and (arr[(i - 1) * m + j + 1] == 1) and (arr[i * m + j + 1] == 1) and (arr[(i - 1) * m + j] == 1) and (arr[i * m + j] == 1):
		return True
	elif j != 0 and i != n - 1 and (arr[(i + 1) * m + j - 1] == 1) and (arr[i * m + j - 1] == 1) and (arr[(i + 1) * m + j] == 1) and (arr[i * m + j] == 1):
		return True
	elif j != m - 1 and i != n - 1 and (arr[(i + 1) * m + j + 1] == 1) and (arr[i * m + j + 1] == 1) and (arr[(i + 1) * m + j] == 1) and (arr[i * m + j] == 1):
		return True
	return False
q508a()


def check(matrix, x, y):
	if matrix[x][y + 1] and matrix[x + 1][y + 1] and matrix[x + 1][y]:
		return True
	if matrix[x][y + 1] and matrix[x - 1][y + 1] and matrix[x - 1][y]:
		return True
	if matrix[x][y - 1] and matrix[x - 1][y - 1] and matrix[x - 1][y]:
		return True
	if matrix[x][y - 1] and matrix[x + 1][y - 1] and matrix[x + 1][y]:
		return True
	return False
(n, m, k) = map(int, input().split())
colors = [[0] * (m + 2) for i in range(n + 2)]
for j in range(k):
	(x, y) = map(int, input().split())
	colors[x][y] = 1
	if check(colors, x, y):
		print(j + 1)
		break
else:
	print(0)


def checkFail(i, j):
	if i - 1 >= 0 and j - 1 >= 0:
		res = 0
		for x in range(i - 1, i + 1):
			for y in range(j - 1, j + 1):
				res += board[x][y]
		if res == 4:
			return True
	if i - 1 >= 0 and j + 1 < m:
		res = 0
		for x in range(i - 1, i + 1):
			for y in range(j, j + 2):
				res += board[x][y]
		if res == 4:
			return True
	if i + 1 < n and j - 1 >= 0:
		res = 0
		for x in range(i, i + 2):
			for y in range(j - 1, j + 1):
				res += board[x][y]
		if res == 4:
			return True
	if i + 1 < n and j + 1 < m:
		res = 0
		for x in range(i, i + 2):
			for y in range(j, j + 2):
				res += board[x][y]
		if res == 4:
			return True
	return False
(n, m, k) = [int(x) for x in input().strip().split()]
board = [[0] * m for i in range(0, n)]
isFail = False
res = 0
for moveIndex in range(0, k):
	(i, j) = [int(x) for x in input().strip().split()]
	if not isFail:
		i -= 1
		j -= 1
		if board[i][j] == 0:
			board[i][j] = 1
			if checkFail(i, j):
				isFail = True
				res = moveIndex + 1
print(res)


(n, m, k) = map(int, input().split())
l = []
for i in range(n + 2):
	l.append([0 for i in range(m + 2)])
ans = 0
done = 'no'
for i in range(k):
	(x, y) = map(int, input().split())
	l[x][y] = 1
	if done == 'no' and (l[x][y - 1] == 1 and l[x - 1][y] == 1 and (l[x - 1][y - 1] == 1) or (l[x][y - 1] == 1 and l[x + 1][y] == 1 and (l[x + 1][y - 1] == 1)) or (l[x - 1][y] == 1 and l[x][y + 1] == 1 and (l[x - 1][y + 1] == 1)) or (l[x + 1][y + 1] == 1 and l[x + 1][y] == 1 and (l[x][y + 1] == 1))):
		ans = i + 1
		done = 'yes'
print(ans)


(n, m, k) = map(int, input().split())
mx = [[0] * (m + 2) for x in range(n + 2)]

def square_check(i, j):
	if mx[i][j + 1] and mx[i + 1][j] and mx[i + 1][j + 1]:
		return True
	if mx[i][j - 1] and mx[i + 1][j] and mx[i + 1][j - 1]:
		return True
	if mx[i - 1][j + 1] and mx[i - 1][j] and mx[i][j + 1]:
		return True
	if mx[i - 1][j - 1] and mx[i - 1][j] and mx[i][j - 1]:
		return True
	return False
num = 0
for i in range(k):
	(x, y) = map(int, input().split())
	mx[x][y] = 1
	if square_check(x, y):
		num = i + 1
		break
print(num)


(n, m, k) = input().split(' ')
n = int(n)
m = int(m)
k = int(k)
l = [[0 for x in range(m + 2)] for x in range(n + 2)]
flag = 0
for i in range(k):
	(a, b) = input().split(' ')
	a = int(a)
	b = int(b)
	l[a][b] = 1
	if l[a][b + 1] == 1 and l[a - 1][b] == 1 and (l[a - 1][b + 1] == 1):
		if flag == 0:
			flag = i + 1
	elif l[a - 1][b] == 1 and l[a - 1][b - 1] == 1 and l[a][b - 1]:
		if flag == 0:
			flag = i + 1
	elif l[a][b - 1] == 1 and l[a + 1][b - 1] == 1 and l[a + 1][b]:
		if flag == 0:
			flag = i + 1
	elif l[a][b + 1] == 1 and l[a + 1][b] and l[a + 1][b + 1]:
		if flag == 0:
			flag = i + 1
print(flag)


n_m_k = input().split()
n = int(n_m_k[0])
m = int(n_m_k[1])
k = int(n_m_k[2])
msv = [[0 for i in range(m)] for j in range(n)]
c = 1
vivod = k + 1
while c <= k:
	vvod = input().split()
	i = int(vvod[0]) - 1
	j = int(vvod[1]) - 1
	msv[i][j] = 1
	if i - 1 > -1 and j + 1 < m:
		if msv[i - 1][j] == 1 and msv[i - 1][j + 1] == 1 and (msv[i][j + 1] == 1):
			if c < vivod:
				vivod = c
	if i + 1 < n and j + 1 < m:
		if msv[i + 1][j] == 1 and msv[i + 1][j + 1] == 1 and (msv[i][j + 1] == 1):
			if c < vivod:
				vivod = c
	if i + 1 < n and j - 1 > -1:
		if msv[i + 1][j - 1] == 1 and msv[i + 1][j] == 1 and (msv[i][j - 1] == 1):
			if c < vivod:
				vivod = c
	if i - 1 > -1 and j - 1 > -1:
		if msv[i - 1][j - 1] == 1 and msv[i - 1][j] == 1 and (msv[i][j - 1] == 1):
			if c < vivod:
				vivod = c
	c += 1
if vivod == k + 1:
	vivod = 0
print(vivod)


import sys
(n, m, k) = map(int, sys.stdin.readline().split())
a = [[0] * (m + 2) for _ in range(n + 2)]
for i in range(k):
	(x, y) = map(int, sys.stdin.readline().split())
	a[x][y] = 1
	if a[x - 1][y - 1] == 1 and a[x - 1][y] == 1 and (a[x][y - 1] == 1):
		print(i + 1)
		break
	if a[x - 1][y + 1] == 1 and a[x - 1][y] == 1 and (a[x][y + 1] == 1):
		print(i + 1)
		break
	if a[x + 1][y - 1] == 1 and a[x + 1][y] == 1 and (a[x][y - 1] == 1):
		print(i + 1)
		break
	if a[x + 1][y + 1] == 1 and a[x + 1][y] == 1 and (a[x][y + 1] == 1):
		print(i + 1)
		break
else:
	print(0)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
(a, b, k) = r()
arr = [[0] * b for _ in range(a)]

def ok(i, j):
	if i > 0 and j < b - 1:
		if arr[i - 1][j] and arr[i - 1][j + 1] and arr[i][j + 1]:
			return True
	if i > 0 and j > 0:
		if arr[i - 1][j] and arr[i - 1][j - 1] and arr[i][j - 1]:
			return True
	if i < a - 1 and j < b - 1:
		if arr[i + 1][j + 1] and arr[i + 1][j] and arr[i][j + 1]:
			return True
	if i < a - 1 and j > 0:
		if arr[i + 1][j] and arr[i + 1][j - 1] and arr[i][j - 1]:
			return True
	return False
for i in range(1, k + 1):
	(x, y) = r()
	x -= 1
	y -= 1
	arr[x][y] = 1
	if ok(x, y):
		exit(print(i))
print(0)


import sys
import math
import bisect

def query(A, x, y):
	n = len(A)
	m = len(A[0])
	if x >= 0 and x + 1 < n and (y >= 0) and (y + 1 < m):
		cnt = 0
		if A[x][y] == 1:
			cnt += 1
		if A[x][y + 1] == 1:
			cnt += 1
		if A[x + 1][y] == 1:
			cnt += 1
		if A[x + 1][y + 1] == 1:
			cnt += 1
		if cnt == 4:
			return True
	return False

def main():
	(n, m, q) = map(int, input().split())
	A = []
	for i in range(n):
		A.append([0] * m)
	ans = 0
	for i in range(q):
		(x, y) = map(int, input().split())
		x -= 1
		y -= 1
		if A[x][y] == 0:
			A[x][y] = 1
			if query(A, x - 1, y - 1):
				ans = i + 1
				break
			if query(A, x - 1, y):
				ans = i + 1
				break
			if query(A, x, y - 1):
				ans = i + 1
				break
			if query(A, x, y):
				ans = i + 1
				break
	print(ans)
main()


(n, m, k) = map(int, input().split())
pole = [0] * (n + 1)
for i in range(n + 1):
	pole[i] = [0] * (m + 1)
g = [0] * (n + 1)
for i in range(n + 1):
	g[i] = [0] * (m + 1)
for i in range(k):
	(a, b) = map(int, input().split())
	if g[a][b] != 1:
		g[a][b] = 1
		pole[a][b] += 1
		pole[a][b - 1] += 1
		pole[a - 1][b] += 1
		pole[a - 1][b - 1] += 1
		if pole[a][b] == 4 or pole[a][b - 1] == 4 or pole[a - 1][b] == 4 or (pole[a - 1][b - 1] == 4):
			print(i + 1)
			break
else:
	print(0)


from sys import stdin, stdout, stderr, setrecursionlimit
setrecursionlimit(100000)

def debug(*e):
	if not __debug__:
		print(*e, file=stderr)

def dd(*vals):
	import inspect, re
	frame = inspect.getframeinfo(inspect.stack()[1][0])
	vs = re.search('dd\\((.+)\\)', frame.code_context[0]).group(1).split(',')
	if vs:
		debug(','.join(('{0} = {1}'.format(vs[i], v) for (i, v) in enumerate(vals))))

def trace(f):

	def traced(*args, **kw):
		debug('calling {} with args {}, {}'.format(f.__name__, args, kw))
		return f(*args, **kw)
	return traced

def read():
	return stdin.readline().rstrip()

def readarr(sep=None, maxsplit=-1):
	return read().split(sep, maxsplit)

def readint():
	return int(read())

def readintarr(sep=None, maxsplit=-1):
	return [int(a) for a in readarr(sep, maxsplit)]

def write(*args, **kwargs):
	sep = kwargs.get('sep', ' ')
	end = kwargs.get('end', '\n')
	stdout.write(sep.join((str(a) for a in args)) + end)

def writearr(arr, sep=' ', end='\n'):
	stdout.write(sep.join((str(a) for a in arr)) + end)
(n, m, k) = readintarr()
d = [[0 for _ in range(m)] for _ in range(n)]
for mov in range(1, k + 1):
	(f, c) = [int(i) - 1 for i in read().split()]
	d[f][c] = 1
	dx = [0, 1, 1]
	dy = [1, 1, 0]
	if all((0 <= f + dx[i] < n and 0 <= c + dy[i] < m and (d[f + dx[i]][c + dy[i]] == 1) for i in range(3))):
		write(mov)
		break
	dx = [0, -1, -1]
	dy = [-1, -1, 0]
	if all((0 <= f + dx[i] < n and 0 <= c + dy[i] < m and (d[f + dx[i]][c + dy[i]] == 1) for i in range(3))):
		write(mov)
		break
	dx = [1, 1, 0]
	dy = [0, -1, -1]
	if all((0 <= f + dx[i] < n and 0 <= c + dy[i] < m and (d[f + dx[i]][c + dy[i]] == 1) for i in range(3))):
		write(mov)
		break
	dx = [-1, -1, 0]
	dy = [0, 1, 1]
	if all((0 <= f + dx[i] < n and 0 <= c + dy[i] < m and (d[f + dx[i]][c + dy[i]] == 1) for i in range(3))):
		write(mov)
		break
else:
	write(0)
dd(d)


(n, m, k) = map(int, input().split())
arr = [[0] * (m + 1) for i in range(n + 1)]
for c in range(k):
	(i, j) = map(int, input().split())
	i -= 1
	j -= 1
	arr[i][j] = 1
	for t1 in range(i - 1, i + 1):
		for t2 in range(j - 1, j + 1):
			if arr[t1][t2] == arr[t1 + 1][t2 + 1] == arr[t1 + 1][t2] == arr[t1][t2 + 1] == 1:
				print(c + 1)
				exit()
print(0)


(n, m, k) = map(int, input().split())

def check(x, y):
	if A[x - 1][y - 1] == 1 and A[x][y - 1] == 1 and (A[x - 1][y] == 1):
		return 1
	elif A[x][y - 1] == 1 and A[x + 1][y - 1] == 1 and (A[x + 1][y] == 1):
		return 1
	elif A[x - 1][y] == 1 and A[x - 1][y + 1] == 1 and (A[x][y + 1] == 1):
		return 1
	elif A[x][y + 1] == 1 and A[x + 1][y] == 1 and (A[x + 1][y + 1] == 1):
		return 1
	else:
		return 0
A = [[0 for i in range(m + 2)] for j in range(n + 2)]
a = 0
for i in range(k):
	(x, y) = map(int, input().split())
	A[x][y] = 1
	if check(x, y) == 1:
		a = 1
		print(i + 1)
		break
if a == 0:
	print(0)


(n, m, k) = [int(x) for x in input().split()]
l = [[int(x) for x in input().split()] for i in range(k)]
if n < 2 or m < 2:
	print(0)
else:
	a = [[0 for i in range(m + 2)] for j in range(n + 2)]
	f = 1
	for i in range(k):
		a[l[i][0]][l[i][1]] = 1
		b = a[l[i][0]][l[i][1]] + a[l[i][0] + 1][l[i][1]] + a[l[i][0]][l[i][1] + 1] + a[l[i][0] + 1][l[i][1] + 1]
		c = a[l[i][0]][l[i][1]] + a[l[i][0] - 1][l[i][1]] + a[l[i][0]][l[i][1] - 1] + a[l[i][0] - 1][l[i][1] - 1]
		d = a[l[i][0]][l[i][1]] + a[l[i][0] + 1][l[i][1]] + a[l[i][0]][l[i][1] - 1] + a[l[i][0] + 1][l[i][1] - 1]
		e = a[l[i][0]][l[i][1]] + a[l[i][0] - 1][l[i][1]] + a[l[i][0]][l[i][1] + 1] + a[l[i][0] - 1][l[i][1] + 1]
		if b == 4 or c == 4 or d == 4 or (e == 4):
			print(i + 1)
			f = 0
			break
	if f == 1:
		print(0)


from sys import stdin, stdout
input = stdin.readline
(n, m, k) = map(int, input().split())
mat = [[False] * (m + 2) for _ in range(n + 2)]
for ans in range(1, k + 1):
	(i, j) = map(int, input().split())
	if mat[i - 1][j] and mat[i][j - 1]:
		if mat[i - 1][j - 1]:
			stdout.write(str(ans))
			exit(0)
	if mat[i - 1][j] and mat[i][j + 1]:
		if mat[i - 1][j + 1]:
			stdout.write(str(ans))
			exit(0)
	if mat[i + 1][j] and mat[i][j - 1]:
		if mat[i + 1][j - 1]:
			stdout.write(str(ans))
			exit(0)
	if mat[i + 1][j] and mat[i][j + 1]:
		if mat[i + 1][j + 1]:
			stdout.write(str(ans))
			exit(0)
	mat[i][j] = True
stdout.write('0')


(n, m, k) = map(int, input().split())
a = [[False] * m for x in range(0, n)]

def check(i, j):
	if i < 0:
		return False
	elif i + 1 >= n:
		return False
	elif j < 0:
		return False
	elif j + 1 >= m:
		return False
	return a[i][j] and a[i][j + 1] and a[i + 1][j] and a[i + 1][j + 1]

def foo():
	for x in range(1, k + 1):
		(i, j) = map(int, input().split())
		i -= 1
		j -= 1
		a[i][j] = True
		if check(i, j) or check(i - 1, j) or check(i, j - 1) or check(i - 1, j - 1):
			print(x)
			return
	print(0)
foo()


(n, m, k) = list(map(int, input().split()))
moves = []
for i in range(k):
	moves.append(list(map(int, input().split())))
g = [[0 for i in range(m)] for i in range(n)]
nbrs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
grps = [[0, 1, 3], [1, 2, 4], [3, 5, 6], [4, 6, 7]]
mc = 1
for i in moves:
	ss = [0 for x in range(8)]
	g[i[0] - 1][i[1] - 1] = 1
	c = 0
	for j in nbrs:
		if (i[0] - 1 + j[0] < n and i[0] - 1 + j[0] >= 0) and (i[1] - 1 + j[1] < m and i[1] - 1 + j[1] >= 0) and (g[i[0] - 1 + j[0]][i[1] - 1 + j[1]] == 1):
			ss[c] = 1
		c += 1
	for j in grps:
		if ss[j[0]] == 1 and ss[j[1]] == 1 and (ss[j[2]] == 1):
			print(mc)
			exit()
	mc += 1
print(0)


def check(a, i1, j1):
	for i in range(max(1, i1 - 1), min(i1 + 2, len(a))):
		for j in range(max(1, j1 - 1), min(j1 + 2, len(a[i]))):
			if a[i][j] and a[i - 1][j] and a[i][j - 1] and a[i - 1][j - 1]:
				return 1
	return 0
(n, m, k) = (int(i) for i in input().split())
a = [[0] * m for i in range(n)]
ans = 0
for c in range(k):
	(i, j) = (int(i) for i in input().split())
	a[i - 1][j - 1] = 1
	if check(a, i - 1, j - 1):
		ans = c + 1
		break
print(ans)


(n, m, k) = map(int, input().split())
l = [[0 for i in range(m + 2)] for j in range(n + 2)]

def check(r, c):
	return l[r][c] and l[r][c + 1] and l[r + 1][c] and l[r + 1][c + 1]
for i in range(k):
	(x, y) = map(int, input().split())
	l[x][y] = 1
	ans = check(x, y) or check(x, y - 1) or check(x - 1, y - 1) or check(x - 1, y)
	if ans:
		print(i + 1)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
r = [[0] * (m + 2) for i in range(n + 2)]
output = 0
for pp in range(k):
	(x, y) = map(int, input().split())
	r[x][y] = 1
	if r[x - 1][y - 1] + r[x][y - 1] + r[x - 1][y] + r[x][y] == 4 or r[x][y - 1] + r[x][y] + r[x + 1][y - 1] + r[x + 1][y] == 4 or r[x - 1][y] + r[x][y] + r[x - 1][y + 1] + r[x][y + 1] == 4 or (r[x][y] + r[x + 1][y] + r[x][y + 1] + r[x + 1][y + 1] == 4):
		output = pp + 1
		break
print(output)


I = lambda : map(int, input().split())
(n, m, k) = I()
m += 2
A = set()
for i in range(k):
	(x, y) = I()
	a = x * m + y
	if {a - m - 1, a - m, a - 1} <= A or {a - m, a - m + 1, a + 1} <= A or {a - 1, a + m - 1, a + m} <= A or ({a + 1, a + m, a + m + 1} <= A):
		print(i + 1)
		break
	A.add(a)
else:
	print(0)


from sys import *
MAXN = 10 ** 5 + 228
MOD = 10 ** 9 + 7

def rect(x, y):
	if d.get((x - 1, y), 0) + d.get((x - 1, y - 1), 0) + d.get((x, y - 1), 0) == 3:
		return 1
	if d.get((x - 1, y), 0) + d.get((x - 1, y + 1), 0) + d.get((x, y + 1), 0) == 3:
		return 1
	if d.get((x + 1, y), 0) + d.get((x + 1, y - 1), 0) + d.get((x, y - 1), 0) == 3:
		return 1
	if d.get((x + 1, y), 0) + d.get((x + 1, y + 1), 0) + d.get((x, y + 1), 0) == 3:
		return 1
	return 0
(n, m, k) = (int(z) for z in input().split())
d = dict()
for i in range(k):
	(u, v) = (int(z) for z in input().split())
	if rect(u, v):
		print(i + 1)
		exit(0)
	d[u, v] = 1
print(0)


from itertools import product
(n, m, k) = map(int, input().split())
D = set()
for q in range(k):
	(a, b) = map(int, input().split())
	D.add((a, b))
	found = False
	for (i, j) in product([-1, 0], repeat=2):
		if all([(a + i + x, b + j + y) in D for (x, y) in product([0, 1], repeat=2)]):
			found = True
	if found:
		print(q + 1)
		break
else:
	print(0)


import itertools
(n, m, k) = map(int, input().split())
field = [[False] * m for index in range(n)]
deltas = (((0, -1), (-1, -1), (-1, 0)), ((-1, 0), (-1, 1), (0, 1)), ((0, 1), (1, 1), (1, 0)), ((1, 0), (1, -1), (0, -1)))
found = False
for index in range(k):
	(i, j) = map(lambda s: int(s) - 1, input().split())
	if not found:
		if not field[i][j]:
			for d in deltas:
				f = lambda di, dj: 0 <= i + di < n and 0 <= j + dj < m and field[i + di][j + dj]
				if all(itertools.starmap(f, d)):
					print(index + 1)
					found = True
					break
		field[i][j] = True
if not found:
	print(0)


(n, m, k) = map(int, input().split())
l = [[0] * (m + 2) for i in range(n + 2)]
for i in range(k):
	(x, y) = map(int, input().split())
	l[x][y] = 1
	if l[x - 1][y - 1] + l[x - 1][y] + l[x][y - 1] == 3:
		print(i + 1)
		break
	elif l[x - 1][y] + l[x][y + 1] + l[x - 1][y + 1] == 3:
		print(i + 1)
		break
	elif l[x + 1][y + 1] + l[x + 1][y] + l[x][y + 1] == 3:
		print(i + 1)
		break
	elif l[x + 1][y - 1] + l[x + 1][y] + l[x][y - 1] == 3:
		print(i + 1)
		break
else:
	print(0)


(n, m, k) = [int(i) for i in input().split()]
l = [(m + 2) * [0] for i in range(n + 2)]
c = 0

def lose(x, y):
	if l[x][y + 1] and l[x + 1][y] and l[x + 1][y + 1]:
		return True
	elif l[x][y + 1] and l[x - 1][y] and l[x - 1][y + 1]:
		return True
	if l[x][y - 1] and l[x + 1][y] and l[x + 1][y - 1]:
		return True
	elif l[x][y - 1] and l[x - 1][y] and l[x - 1][y - 1]:
		return True
	else:
		return False
for i in range(k):
	(x, y) = map(int, input().split())
	l[x][y] = 1
	if lose(x, y) == 1:
		print(i + 1)
		c = 1
		break
if c == 0:
	print(0)


__author__ = 'artyom'
read = lambda : map(int, input().split())
(n, m, k) = read()
a = [[False] * (m + 2) for _ in range(n + 2)]
for t in range(k):
	(i, j) = read()
	a[i][j] = True
	if any([all((a[i + p][j + q] for (p, q) in cs)) for cs in [[[-1, -1], [-1, 0], [0, -1]], [[0, -1], [1, -1], [1, 0]], [[-1, 0], [-1, 1], [0, 1]], [[0, 1], [1, 0], [1, 1]]]]):
		print(t + 1)
		exit()
print(0)


(n, m, k) = map(int, input().split())
game = [[0] * (m + 2) for i in range(n + 2)]

def check(mx, i, j):
	if mx[i][j + 1] + mx[i + 1][j] + mx[i + 1][j + 1] == 3:
		return False
	if mx[i][j - 1] + mx[i + 1][j] + mx[i + 1][j - 1] == 3:
		return False
	if mx[i - 1][j] + mx[i - 1][j + 1] + mx[i][j + 1] == 3:
		return False
	if mx[i - 1][j - 1] + mx[i - 1][j] + mx[i][j - 1] == 3:
		return False
	return True
for i in range(k):
	(x, y) = map(int, input().split())
	game[x][y] = 1
	if check(game, x, y) == False:
		print(i + 1)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
a = []
for i in range(n + 2):
	a.append([0 for i in range(m + 2)])
for q in range(k):
	(i, j) = map(int, input().split())
	a[i][j] = 1
	cnt = a[i][j] + a[i - 1][j] + a[i - 1][j + 1] + a[i][j + 1]
	if cnt == 4:
		print(q + 1)
		exit()
	cnt = a[i][j] + a[i - 1][j - 1] + a[i - 1][j] + a[i][j - 1]
	if cnt == 4:
		print(q + 1)
		exit()
	cnt = a[i][j] + a[i][j - 1] + a[i + 1][j - 1] + a[i + 1][j]
	if cnt == 4:
		print(q + 1)
		exit()
	cnt = a[i][j] + a[i][j + 1] + a[i + 1][j] + a[i + 1][j + 1]
	if cnt == 4:
		print(q + 1)
		exit()
print(0)


bad = []
x = 0
y = 0
asdf = [[False] * 1005 for _ in range(1005)]
(a, b, c) = map(int, input().split(' '))
for i in range(c):
	(x, y) = map(int, input().split(' '))
	asdf[x][y] = True
	if asdf[x - 1][y - 1] and asdf[x][y - 1] and asdf[x - 1][y]:
		print(i + 1)
		quit()
	elif asdf[x - 1][y] and asdf[x][y + 1] and asdf[x - 1][y + 1]:
		print(i + 1)
		quit()
	elif asdf[x][y - 1] and asdf[x + 1][y - 1] and asdf[x + 1][y]:
		print(i + 1)
		quit()
	elif asdf[x][y + 1] and asdf[x + 1][y + 1] and asdf[x + 1][y]:
		print(i + 1)
		quit()
print(0)


(n, m, k) = map(int, input().split())
a = [[0] * (m + 2) for i in range(n + 2)]
for c in range(k):
	(i, j) = map(int, input().split())
	i -= 1
	j -= 1
	a[i][j] = 1
	for i1 in range(i - 1, i + 1):
		for j1 in range(j - 1, j + 1):
			if a[i1][j1] == a[i1 + 1][j1 + 1] == a[i1 + 1][j1] == a[i1][j1 + 1] == 1:
				print(c + 1)
				return
print(0)


__author__ = 'default'

def TaskA():
	(n, m, k) = list(map(int, input().split()))
	pole = [0] * n
	lose = False
	cmplt = True
	for i in range(n):
		pole[i] = [0] * m
	for i in range(k):
		rdl = list(map(int, input().split()))
		pole[rdl[0] - 1][rdl[1] - 1] = 1
		lose = check(pole, rdl[0] - 1, rdl[1] - 1)
		if lose and cmplt:
			cmplt = False
			print(i + 1)
	if not lose and cmplt:
		print(0)

def check(pole, index1, index):
	if index1 - 1 >= 0 and index + 1 < len(pole[0]):
		if pole[index1][index + 1] == 1 and pole[index1 - 1][index] == 1 and (pole[index1 - 1][index + 1] == 1):
			return True
	if index1 + 1 < len(pole) and index - 1 > 0 >= 0:
		if pole[index1][index - 1] == 1 and pole[index1 + 1][index - 1] == 1 and (pole[index1 + 1][index] == 1):
			return True
	if index1 - 1 >= 0 and index - 1 >= 0:
		if pole[index1 - 1][index] == 1 and pole[index1 - 1][index - 1] == 1 and (pole[index1][index - 1] == 1):
			return True
	if index1 + 1 < len(pole) and index + 1 < len(pole[0]):
		if pole[index1 + 1][index] == 1 and pole[index1 + 1][index + 1] == 1 and pole[index1][index + 1]:
			return True
TaskA()


def formed(w, e):
	if w + 1 < n and mat[w + 1][e] == 'B':
		if e + 1 < m and mat[w + 1][e + 1] == 'B' and (mat[w][e + 1] == 'B') or (e - 1 >= 0 and mat[w + 1][e - 1] == 'B' and (mat[w][e - 1] == 'B')):
			return True
	elif w - 1 >= 0 and mat[w - 1][e] == 'B':
		if e + 1 < m and mat[w - 1][e + 1] == 'B' and (mat[w][e + 1] == 'B') or (e - 1 >= 0 and mat[w - 1][e - 1] == 'B' and (mat[w][e - 1] == 'B')):
			return True
	return False
(n, m, k) = [int(x) for x in input().split()]
mat = [[] for l in range(n)]
outcome = 0
for p in range(n):
	for t in range(m):
		mat[p].append('W')
for g in range(k):
	(i, j) = [int(x) - 1 for x in input().split()]
	mat[i][j] = 'B'
	if formed(i, j):
		outcome = g + 1
		break
print(outcome)


(R, M, K) = [int(x) for x in input().split()]
matrix = [[False for j in range(M)] for i in range(R)]
for i in range(K):
	(r, c) = [int(x) - 1 for x in input().split()]
	matrix[r][c] = True
	flg = False
	if r > 0 and matrix[r - 1][c]:
		if c > 0:
			if matrix[r - 1][c - 1] and matrix[r][c - 1]:
				flg = True
		if c < M - 1:
			if matrix[r - 1][c + 1] and matrix[r][c + 1]:
				flg = True
	if r < R - 1 and matrix[r + 1][c]:
		if c > 0:
			if matrix[r + 1][c - 1] and matrix[r][c - 1]:
				flg = True
		if c < M - 1:
			if matrix[r + 1][c + 1] and matrix[r][c + 1]:
				flg = True
	if flg:
		print(i + 1)
		return
print(0)


firstLine = [int(n) for n in input().split()]
matrix = []
for i in range(0, firstLine[0]):
	matrix.append([])
	for j in range(0, firstLine[1]):
		matrix[i].append(0)
for i in range(0, firstLine[2]):
	temp = [int(n) for n in input().split()]
	y = temp[0] - 1
	x = temp[1] - 1
	matrix[y][x] = 1
	out = True
	if y - 1 >= 0:
		if x - 1 >= 0:
			if matrix[y - 1][x - 1] + matrix[y][x - 1] + matrix[y - 1][x] + matrix[y][x] == 4 and out == True:
				print(i + 1)
				out = False
				break
		if x + 1 < len(matrix[0]):
			if matrix[y - 1][x + 1] + matrix[y][x + 1] + matrix[y - 1][x] + matrix[y][x] == 4 and out == True:
				print(i + 1)
				out = False
				break
	if y + 1 < len(matrix):
		if x - 1 >= 0:
			if matrix[y + 1][x - 1] + matrix[y][x - 1] + matrix[y + 1][x] + matrix[y][x] == 4 and out == True:
				print(i + 1)
				out = False
				break
		if x + 1 < len(matrix[0]):
			if matrix[y + 1][x + 1] + matrix[y][x + 1] + matrix[y + 1][x] + matrix[y][x] == 4 and out == True:
				print(i + 1)
				out = False
				break
if out == True:
	print(0)


(n, m, k) = [int(i) for i in input().split()]
B = [[0 for x in range(m + 2)] for y in range(n + 2)]
a = True
b = 0

def check(x, y):
	global a, b
	if B[x][y] + B[x - 1][y] + B[x][y - 1] + B[x - 1][y - 1] == 4 or B[x][y] + B[x + 1][y] + B[x][y - 1] + B[x + 1][y - 1] == 4 or B[x][y] + B[x - 1][y] + B[x][y + 1] + B[x - 1][y + 1] == 4 or (B[x][y] + B[x + 1][y] + B[x][y + 1] + B[x + 1][y + 1] == 4):
		b = i + 1
		a = False
for i in range(k):
	(c, d) = [int(t) for t in input().split()]
	B[c][d] = 1
	if i > 2 and a == True:
		check(c, d)
print([0, b][a == False])


string = input()
(a, b, n) = map(int, string.split())
rows = ['0' * b for x in range(a)]
m = 0

def check(x):
	global condition
	if s[x] == '1':
		templist.append(x)
		condition = True

def checkrow(x):
	global m
	t = rows[x]
	for y in templist:
		if t[c] == t[y] == '1':
			m = black + 1
			break
for x in range(n):
	black = x
	string = input()
	numbers = string.split()
	(r, c) = (int(numbers[0]) - 1, int(numbers[1]) - 1)
	s = rows[r]
	rows[r] = s[:c] + '1' + s[c + 1:]
	s = rows[r]
	if m == 0:
		templist = []
		condition = False
		if c != b - 1:
			check(c + 1)
		if c != 0:
			check(c - 1)
		if condition:
			if r != a - 1:
				checkrow(r + 1)
			if r != 0:
				checkrow(r - 1)
print(m)


(n, m, k) = map(int, input().split())
mx = [[0] * (m + 2) for i in range(n + 2)]
for i in range(k):
	(x, y) = map(int, input().split())
	mx[x][y] = 1
	if mx[x - 1][y - 1] + mx[x - 1][y] + mx[x][y - 1] == 3 or mx[x][y - 1] + mx[x + 1][y - 1] + mx[x + 1][y] == 3 or mx[x - 1][y] + mx[x - 1][y + 1] + mx[x][y + 1] == 3 or (mx[x + 1][y] + mx[x][y + 1] + mx[x + 1][y + 1] == 3):
		print(i + 1)
		break
else:
	print(0)


def checker(a, x, y):
	if a[x - 1][y - 1] == 1 and a[x - 1][y] == 1 and (a[x][y - 1] == 1):
		return 1
	if a[x - 1][y] == 1 and a[x - 1][y + 1] == 1 and (a[x][y + 1] == 1):
		return 1
	if a[x][y - 1] == 1 and a[x + 1][y - 1] == 1 and (a[x + 1][y] == 1):
		return 1
	if a[x][y + 1] == 1 and a[x + 1][y] == 1 and (a[x + 1][y + 1] == 1):
		return 1
(n, m, k) = map(int, input().split())
a = []
for i in range(n + 2):
	a.append([0] * (m + 2))
for i in range(k):
	(x, y) = map(int, input().split())
	if checker(a, x, y):
		print(i + 1)
		exit()
	else:
		a[x][y] = 1
print(0)


(n, m, k) = (int(x) for x in input().split())

class Field:

	def __init__(self):
		self.elements = {}

	def __getitem__(self, id):
		return self.elements.get(id, False)

	def __setitem__(self, id, value):
		self.elements[id] = value
f = Field()
for ki in range(k):
	(i, j) = (int(x) for x in input().split())
	f[i, j] = True
	if f[i - 1, j] and f[i, j - 1] and f[i - 1, j - 1]:
		print(ki + 1)
		break
	if f[i - 1, j] and f[i, j + 1] and f[i - 1, j + 1]:
		print(ki + 1)
		break
	if f[i + 1, j] and f[i, j - 1] and f[i + 1, j - 1]:
		print(ki + 1)
		break
	if f[i + 1, j] and f[i, j + 1] and f[i + 1, j + 1]:
		print(ki + 1)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
l1 = [[]]
s = 0
c = 0
M = [[0 for i in range(m + 5)] for i in range(n + 5)]
for i in range(k):
	(a, b) = map(int, input().split())
	s = s + 1
	a = a + 2
	b = b + 2
	M[a - 1][b - 1] = 1
	if M[a - 2][b - 1] == 1 and M[a - 2][b - 2] == 1 and (M[a - 1][b - 2] == 1):
		print(s)
		c = 1
		break
	if M[a - 2][b - 1] == 1 and M[a - 2][b] == 1 and (M[a - 1][b] == 1):
		print(s)
		c = 1
		break
	if M[a][b - 1] == 1 and M[a][b - 2] == 1 and (M[a - 1][b - 2] == 1):
		print(s)
		c = 1
		break
	if M[a][b - 1] == 1 and M[a][b] == 1 and (M[a - 1][b] == 1):
		print(s)
		c = 1
		break
if c == 0:
	print(0)


(n, m, k) = map(int, input().split())
mat = []
done = False
for i in range(n + 10):
	lol = []
	for j in range(m + 10):
		lol.append(False)
	mat.append(lol)
for i in range(k):
	ok = False
	(x, y) = map(int, input().split())
	mat[x][y] = True
	try:
		if mat[x][y] and mat[x - 1][y] and mat[x - 1][y - 1] and mat[x][y - 1]:
			ok = True
	except:
		pass
	try:
		if mat[x][y] and mat[x - 1][y] and mat[x - 1][y + 1] and mat[x][y + 1]:
			ok = True
	except:
		pass
	try:
		if mat[x][y] and mat[x + 1][y] and mat[x + 1][y - 1] and mat[x][y - 1]:
			ok = True
	except:
		pass
	try:
		if mat[x][y] and mat[x + 1][y] and mat[x + 1][y + 1] and mat[x][y + 1]:
			ok = True
	except:
		pass
	if ok:
		done = True
		print(i + 1)
		break
if not done:
	print(0)


(n, m, k) = map(int, input().split())
li = [[0 for j in range(m + 2)] for i in range(n + 2)]
c = u = 0
for i in range(k):
	(a, b) = map(int, input().split())
	li[a][b] = 1
	c += 1
	if li[a - 1][b - 1] == 1 and li[a - 1][b] == 1 and (li[a][b - 1] == 1):
		print(c)
		u = 1
		break
	elif li[a - 1][b + 1] == 1 and li[a - 1][b] == 1 and (li[a][b + 1] == 1):
		print(c)
		u = 1
		break
	elif li[a + 1][b - 1] == 1 and li[a + 1][b] == 1 and (li[a][b - 1] == 1):
		print(c)
		u = 1
		break
	elif li[a + 1][b + 1] == 1 and li[a + 1][b] == 1 and (li[a][b + 1] == 1):
		print(c)
		u = 1
		break
if u != 1:
	print('0')


(n, m, k) = [int(x) for x in input().split()]
matrix = [[0] * (m + 2) for i in range(n + 2)]
ans = 0
num = 0
for q in range(k):
	ans += 1
	(i, j) = [int(x) for x in input().split()]
	matrix[i][j] = 1
	sur1 = matrix[i][j] + matrix[i][j + 1] + matrix[i - 1][j] + matrix[i - 1][j + 1]
	sur2 = matrix[i][j] + matrix[i][j + 1] + matrix[i + 1][j] + matrix[i + 1][j + 1]
	sur3 = matrix[i][j] + matrix[i][j - 1] + matrix[i - 1][j] + matrix[i - 1][j - 1]
	sur4 = matrix[i][j] + matrix[i][j - 1] + matrix[i + 1][j] + matrix[i + 1][j - 1]
	if sur1 == 4 or sur2 == 4 or sur3 == 4 or (sur4 == 4):
		num += 1
		break
if num == 0:
	print('0')
else:
	print(ans)


(n, m, k) = list(map(int, input().split()))
step = [list(map(int, input().split())) for i in range(k)]
numberstep = 0
(black1, black2, black3, black4) = [0] * 4
screen = [[0 for j in range(m + 2)] for i in range(n + 2)]
for i in range(k):
	numberstep += 1
	xs = step[i][0]
	ys = step[i][1]
	screen[step[i][0]][step[i][1]] = 1
	black1 = screen[xs - 1][ys] + screen[xs][ys] + screen[xs - 1][ys + 1] + screen[xs][ys + 1]
	black2 = screen[xs][ys + 1] + screen[xs + 1][ys + 1] + screen[xs][ys] + screen[xs + 1][ys]
	black3 = screen[xs][ys] + screen[xs + 1][ys] + screen[xs][ys - 1] + screen[xs + 1][ys - 1]
	black4 = screen[xs - 1][ys] + screen[xs][ys] + screen[xs - 1][ys - 1] + screen[xs][ys - 1]
	if black1 == 4 or black2 == 4 or black3 == 4 or (black4 == 4):
		print(numberstep)
		break
else:
	print(0)


(n, m, k) = map(int, input().split())
matrix = []
for i in range(n + 2):
	matrix.append([0] * (m + 2))
l = []
for i in range(k):
	l.append(tuple(map(int, input().split())))
i = 1
for (x, y) in l:
	if matrix[x][y] == 0:
		if min(matrix[x - 1][y], matrix[x - 1][y - 1], matrix[x][y - 1] == 1):
			print(i)
			exit()
		elif min(matrix[x][y - 1], matrix[x + 1][y - 1], matrix[x + 1][y] == 1):
			print(i)
			exit()
		elif min(matrix[x + 1][y], matrix[x + 1][y + 1], matrix[x][y + 1] == 1):
			print(i)
			exit()
		elif min(matrix[x][y + 1], matrix[x - 1][y + 1], matrix[x - 1][y] == 1):
			print(i)
			exit()
		else:
			matrix[x][y] = 1
	i += 1
print(0)


(n, m, k) = map(int, input().split())
line = []
for i in range(n + 2):
	line.append([0] * (m + 2))
for i in range(k):
	(a, b) = map(int, input().split())
	a = a - 1
	b = b - 1
	line[a + 1][b + 1] = 1
	if line[a][b] + line[a + 1][b] + line[a][b + 1] == 3:
		print(i + 1)
		exit(0)
	if line[a + 2][b] + line[a + 1][b] + line[a + 2][b + 1] == 3:
		print(i + 1)
		exit(0)
	if line[a + 2][b + 2] + line[a + 2][b + 1] + line[a + 1][b + 2] == 3:
		print(i + 1)
		exit(0)
	if line[a][b + 2] + line[a + 1][b + 2] + line[a][b + 1] == 3:
		print(i + 1)
		exit(0)
print(0)


(n, m, k) = map(int, input().split())
arr = [[0 for _ in range(m)] for _ in range(n)]
for i in range(1, k + 1):
	(a, b) = map(int, input().split())
	a = a - 1
	b = b - 1
	if arr[a][b] == 0:
		arr[a][b] = i
ans = 100000000000
for i in range(n - 1):
	for j in range(m - 1):
		if arr[i][j] > 0 and arr[i + 1][j] > 0 and (arr[i][j + 1] > 0) and (arr[i + 1][j + 1] > 0):
			maxval = max(arr[i][j], max(arr[i][j + 1], max(arr[i + 1][j], arr[i + 1][j + 1])))
			ans = min(ans, maxval)
print(ans if ans != 100000000000 else 0)


(n, m, k) = map(int, input().split())
c1 = 0
a = [[False] * (m + 1) for i in range(n + 1)]
g = 0
for i in range(k):
	(x, y) = map(int, input().split())
	a[x][y] = True
	if c1 != -1:
		s = 0
		for j in range(max(1, x - 1), x + 1):
			for r in range(max(1, y - 1), y + 1):
				if a[j][r] == True:
					s = s + 1
		if s == 4:
			c1 = -1
			g = i + 1
		s = 0
		for j in range(max(1, x - 1), x + 1):
			for r in range(y, min(y + 1, len(a[j]) - 1) + 1):
				if a[j][r] == True:
					s = s + 1
		if s == 4:
			c1 = -1
			g = i + 1
		s = 0
		for j in range(x, min(x + 1, len(a) - 1) + 1):
			for r in range(max(1, y - 1), y + 1):
				if a[j][r] == True:
					s = s + 1
		if s == 4:
			c1 = -1
			g = i + 1
		s = 0
		for j in range(x, min(x + 1, len(a) - 1) + 1):
			for r in range(y, min(y + 1, len(a[j]) - 1) + 1):
				if a[j][r] == True:
					s = s + 1
		if s == 4:
			c1 = -1
			g = i + 1
print(g)


(n, m, k) = map(int, input().split())
a = [[2] + [0] * m + [2] for i in range(n)]
a.insert(0, [2] * (m + 2))
a.insert(n + 1, [2] * (m + 2))
for x in range(1, k + 1):
	(i, j) = map(int, input().split())
	i -= 1
	j -= 1
	a[i][j] = 1
	if a[i][j] == a[i][j + 1] == 1:
		if a[i + 1][j] == a[i + 1][j + 1] == 1:
			print(x)
			break
		elif a[i - 1][j] == a[i - 1][j + 1] == 1:
			print(x)
			break
	if a[i][j] == a[i][j - 1]:
		if a[i + 1][j] == a[i + 1][j - 1] == 1:
			print(x)
			break
		elif a[i - 1][j] == a[i - 1][j - 1] == 1:
			print(x)
			break
else:
	print(0)


I = []
J = []
(n, m, k) = map(int, input().split())
a = [[0] * m for i in range(n)]
k1 = k
while k:
	k -= 1
	(i, j) = map(int, input().split())
	I.append(i - 1)
	J.append(j - 1)
flag = False
for c in range(k1):
	i = I[c]
	j = J[c]
	if a[i][j] == 0:
		a[i][j] = 1
		if i >= 1 and j >= 1:
			if a[i][j - 1] == 1 and a[i - 1][j - 1] == 1 and (a[i - 1][j] == 1):
				flag = True
		if i >= 1 and j <= m - 2:
			if a[i - 1][j] == 1 and a[i - 1][j + 1] == 1 and (a[i][j + 1] == 1):
				flag = True
		if j >= 1 and i <= n - 2:
			if a[i][j - 1] == 1 and a[i + 1][j - 1] == 1 and (a[i + 1][j] == 1):
				flag = True
		if j <= m - 2 and i <= n - 2:
			if a[i + 1][j] == 1 and a[i + 1][j + 1] == 1 and (a[i][j + 1] == 1):
				flag = True
	if flag:
		break
if flag:
	print(c + 1)
else:
	print(0)


(n, m, k) = map(int, input().split())
A = [[0] * (m + 1) for i in range(n + 1)]
counter = 0
flag = True
for i in range(k):
	counter += 1
	(i, j) = map(lambda x: int(x) - 1, input().split())
	A[i][j] = 1
	if A[i][j - 1] == 1 and A[i - 1][j - 1] == 1 and (A[i - 1][j] == 1):
		print(counter)
		flag = False
		break
	if A[i - 1][j] == 1 and A[i - 1][j + 1] == 1 and (A[i][j + 1] == 1):
		print(counter)
		flag = False
		break
	if A[i][j - 1] == 1 and A[i + 1][j - 1] == 1 and (A[i + 1][j] == 1):
		print(counter)
		flag = False
		break
	if A[i][j + 1] == 1 and A[i + 1][j] == 1 and (A[i + 1][j + 1] == 1):
		print(counter)
		flag = False
		break
if flag == True:
	print(0)


(n, m, k) = map(int, input().split())
s = set()
dy = [-1, -1, 0, 0]
dx = [-1, 0, -1, 0]
for i in range(k):
	(y, x) = map(int, input().split())
	s |= {(y, x)}
	for k in range(4):
		c = 0
		for u in range(2):
			for v in range(2):
				a = y + dy[k] + u
				b = x + dx[k] + v
				if (a, b) in s:
					c += 1
		if c == 4:
			print(i + 1)
			exit(0)
	k -= 1
print(0)


(n, m, k) = map(int, input().split())
A = [[0] * (m + 2) for _ in range(2 + n)]
for iy in range(k):
	(a, b) = map(int, input().split())
	A[a][b] = 1
	q = A[a - 1][b - 1]
	w = A[a - 1][b]
	e = A[a - 1][b + 1]
	r = A[a][b - 1]
	t = A[a][b + 1]
	y = A[a + 1][b - 1]
	u = A[a + 1][b]
	i = A[a + 1][b + 1]
	if q == w == r == 1 or w == e == t == 1 or t == u == i == 1 or (r == y == u == 1):
		print(iy + 1)
		exit(0)
print(0)


(n, m, k) = map(int, input().split())
a = [[False] * m for _ in range(n)]
for i in range(k):
	(x, y) = map(int, input().split())
	if a[x - 1][y - 1] == True:
		continue
	a[x - 1][y - 1] = True
	if x > 1 and y > 1:
		if a[x - 1][y - 1] == a[x - 2][y - 2] == a[x - 1][y - 2] == a[x - 2][y - 1] == True:
			print(i + 1)
			quit()
	if x < n and y > 1:
		if a[x - 1][y - 1] == a[x][y - 2] == a[x][y - 1] == a[x - 1][y - 2] == True:
			print(i + 1)
			quit()
	if x > 1 and y < m:
		if a[x - 1][y - 1] == a[x - 2][y] == a[x - 2][y - 1] == a[x - 1][y] == True:
			print(i + 1)
			quit()
	if x < n and y < m:
		if a[x - 1][y - 1] == a[x][y] == a[x][y - 1] == a[x - 1][y] == True:
			print(i + 1)
			quit()
print(0)