using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (h, w, n) = (c[0], c[1], c[2]); var map = NArr(h); var tree = new List<(int to, int len)>[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int len)>(); for (var i = 0; i + 1 < n; ++i) tree[i].Add((i + 1, 0)); for (var i = 0; i < h; ++i) for (var j = 0; j + 1 < w; ++j) { if (map[i][j] < map[i][j + 1]) tree[map[i][j] - 1].Add((map[i][j + 1] - 1, 1)); else if (map[i][j] > map[i][j + 1]) tree[map[i][j + 1] - 1].Add((map[i][j] - 1, 1)); } for (var i = 0; i + 1 < h; ++i) for (var j = 0; j < w; ++j) { if (map[i][j] < map[i + 1][j]) tree[map[i][j] - 1].Add((map[i + 1][j] - 1, 1)); else if (map[i][j] > map[i + 1][j]) tree[map[i + 1][j] - 1].Add((map[i][j] - 1, 1)); } var len = new int[n]; for (var i = 0; i + 1 < n; ++i) { foreach (var next in tree[i]) { len[next.to] = Math.Max(len[next.to], len[i] + next.len); } } WriteLine(new HashSet(len).Count); } }