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

Difficulty: interview

Starter code:

Solutions:
"""
Codeforces Contest 288 Div 2 Problem A

Author  : chaotic_iak
Language: Python 3.4.2
"""

################################################### SOLUTION

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


#################################################### HELPERS



def read(mode=2):
    # 0: String
    # 1: List of strings
    # 2: List of integers
    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())

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 = list(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 = list(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)


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)

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)

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)

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)

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)

import itertools

n, m, k = list(map(int, input().split()))
field = [[False] * m for index in range(n)]
deltas = (
    ((0, -1), (-1, -1), (-1, 0)), # Top left.
    ((-1, 0), (-1, 1), (0, 1)),   # Top right.
    ((0, 1), (1, 1), (1, 0)),     # Bottom right.
    ((1, 0), (1, -1), (0, -1)),   # Bottom left.
)
found = False
for index in range(k):
    i, j = [int(s) - 1 for s in 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())
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)

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)


def __starting_point():
    main()
__starting_point()

n,m,k = list(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)



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
            #print( board )
            if checkFail( i , j ):
                isFail = True
                res = moveIndex + 1
                #print( "found!!!" + str(res) )
print(res)

    


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():
    #fl = open('TaskA.txt','r')
    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)
    #fl.close()
    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()

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)

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: #up right
  if flag==0:
   flag=i+1
 elif l[a-1][b]==1 and l[a-1][b-1]==1 and l[a][b-1]:#up left
  if flag==0:
   flag=i+1
 elif l[a][b-1]==1 and l[a+1][b-1]==1 and l[a+1][b]:#down left
  if flag==0:
   flag=i+1
 elif l[a][b+1]==1 and l[a+1][b] and l[a+1][b+1]:#down right
  if flag==0:
   flag=i+1
print(flag)


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)

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 
            else:
                if a < res:
                    res = a
            
print(res)



def Main():
    string = (input()).split(" ")
    n = int(string[0]) 
    m = int(string[1])
    k = int(string[2])
    
    # exit immediately if smth wrong
    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
        
    #   i = i -1
    #   j = j -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

def __starting_point():
    Main()

__starting_point()

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)
    


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
     #for i in range(0,n):
     #   print(l[i])
if kk==0:
    print(0)

    


n,m,k=list(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=list(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)
                return
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 = list(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 = list(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)