import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static int[][] dp; static int[][] field; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt(); int w = sc.nextInt(); field = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { field[i][j] = sc.nextInt(); } } dp = new int[h][w]; int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (dp[i][j] == 0) { dp[i][j] = dfw(i, j); } ans = Math.max(ans, dp[i][j]); } } System.out.println(ans); } static int dfw(int r, int c) { if (dp[r][c] == 0) { int current = 0; if (r > 0 && field[r - 1][c] < field[r][c]) { current = Math.max(current, dfw(r - 1, c)); } if (r < field.length - 1 && field[r + 1][c] < field[r][c]) { current = Math.max(current, dfw(r + 1, c)); } if (c > 0 && field[r][c - 1] < field[r][c]) { current = Math.max(current, dfw(r, c - 1)); } if (c < field[r].length - 1 && field[r][c + 1] < field[r][c]) { current = Math.max(current, dfw(r, c + 1)); } dp[r][c] = current + 1; } return dp[r][c]; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }