mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline d = [(0, 1), (0, -1), (-1, 0), (1, 0)] H, W, N = map(int, input().split()) C = [] for _ in range(H): C.append(list(map(int, input().split()))) P = [[] for _ in range(N+1)] for h in range(H): for w in range(W): P[C[h][w]].append((h, w)) ans = 1 prev = 1 for c in range(2, N+1): flg = 0 for h, w in P[c]: for dh, dw in d: h_new, w_new = h + dh, w + dw if 0 <= h_new < H and 0 <= w_new < W: if C[h_new][w_new] == prev: flg = 1 if flg: ans += 1 prev = c print(ans) if __name__ == '__main__': main()