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) elif a[i][j] > a[i + 1][j]: g[a[i + 1][j] - 1].append(a[i][j] - 1) if j < w - 1: if a[i][j] < a[i][j + 1]: g[a[i][j] - 1].append(a[i][j + 1] - 1) elif a[i][j] > a[i][j + 1]: g[a[i][j + 1] - 1].append(a[i][j] - 1) d = [0] * n for p in range(n): if p > 0 and d[p] < d[p - 1]: d[p] = d[p - 1] for node in g[p]: if d[node] < d[p] + 1: d[node] = d[p] + 1 print(max(d) + 1)