import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
new Solution().maxTotalFruits();
}
}
class Solution {
final int mod = (int) 1e9 + 7, N = (int) 110, M = 200, INF = 0x3f3f3f3f;
public void maxTotalFruits() throws Exception {
Read read = new Read();
int n = read.nextInt();
int m = read.nextInt();
int[][] g = new int[n + 10][m + 10];
int[][] f = new int[n + 10][m + 10];
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= m ; j++)
g[i][j] = read.nextInt();
int ans = 1;
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
f[i][j] = 1;
if(g[i][j] == g[i-1][j] || g[i][j - 1] == g[i][j])continue;
f[i][j] = Math.min(Math.min(f[i-1][j],f[i][j-1]),f[i-1][j-1]) + 1;
ans = Math.max(ans,f[i][j]);
}
}
System.out.println(ans);
}
}
class Read {
public BufferedReader in;
public StringTokenizer tok;
public PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
public Read() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public boolean hasNext() {
while (tok == null || !tok.hasMoreTokens())
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return false;
}
return true;
}
public String next() {
hasNext();
return tok.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}