h, w, n = map(int, input().split()) c = [list(map(int, input().split())) for _ in range(h)] for i in range(h): for j in range(w): c[i][j] -= 1 to = [[] for _ in range(n)] for i in range(h - 1): for j in range(w): x = c[i][j] y = c[i + 1][j] if x < y: to[x].append(y) elif x > y: to[y].append(x) for i in range(h): for j in range(w - 1): x = c[i][j] y = c[i][j + 1] if x < y: to[x].append(y) elif x > y: to[y].append(x) dp = [0] * n for i in range(n): for j in to[i]: dp[j] = max(dp[j], dp[i] + 1) ans = max(dp) print(ans + 1)