h, w, n = map(int, input().split()) a = [list(map(int, input().split())) for _ in range(h)] inv = [0] * n g = [[] for _ in range(n)] for i in range(h): for j in range(w): if i < h - 1: if a[i][j] < a[i + 1][j]: g[a[i][j] - 1].append(a[i + 1][j] - 1) inv[a[i + 1][j] - 1] += 1 elif a[i][j] > a[i + 1][j]: g[a[i + 1][j] - 1].append(a[i][j] - 1) inv[a[i][j] - 1] += 1 if j < w - 1: if a[i][j] < a[i][j + 1]: g[a[i][j] - 1].append(a[i][j + 1] - 1) inv[a[i][j + 1] - 1] += 1 elif a[i][j] > a[i][j + 1]: g[a[i][j + 1] - 1].append(a[i][j] - 1) inv[a[i][j] - 1] += 1 s = [] d = [0] * n for i in range(n): if inv[i] == 0: s.append(i) while s: p = s.pop() for node in set(g[p]): if d[node] < d[p] + 1: d[node] = d[p] + 1 inv[node] -= 1 if inv[node] == 0: s.append(node) print(max(d) + 1)