H,W,N=map(int,input().split());C=[list(map(int,input().split()))for _ in[0]*H];d=[[]for _ in[0]*N];a=s=1 for i in range(H): for j in range(W):d[C[i][j]-1].append((i,j)) 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