from sys import stdin pin=stdin.readline H,W,N=map(int,pin().split()) C=[list(map(int,pin().split()))for _ in[0]*H] d=[[]for _ in[0]*N] for i in range(H): for j in range(W): d[C[i][j]-1].append((i,j)) a=s=1 for i in range(N): f=0 for x,y in d[i]: for u,t in ((1,0),(0,1),(-1,0),(0,-1)): if 0<=x+u