import java.util.LinkedHashSet;
import java.util.Scanner;
public class P1987 {
private static LinkedHashSet<Character> set;
private static char[][] matrix;
private static boolean[][] visit;
private static int[] dx = { 0, 0, -1, 1 };
private static int[] dy = { 1, -1, 0, 0 };
private static int r, c;
private static int answer;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
set = new LinkedHashSet<>();
r = sc.nextInt();
c = sc.nextInt();
matrix = new char[r][c];
visit = new boolean[r][c];
for (int i = 0; i < r; i++) {
char[] input = sc.next().toCharArray();
for (int j = 0; j < c; j++) {
matrix[i][j] = input[j];
}
}
set.add(matrix[0][0]);
dfs(0, 0, 1);
System.out.println(answer);
sc.close();
}
private static void dfs(int x, int y, int max) {
answer = answer < max ? max : answer;
if (visit[x][y]) return ;
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if (isValid(newX, newY) && !set.contains(matrix[newX][newY])) {
visit[x][y] = true;
set.add(matrix[newX][newY]);
dfs(newX, newY, max + 1);
visit[x][y] = false;
set.remove(matrix[newX][newY]);
}
}
}
private static boolean isValid(int x, int y) {
return x < 0 || x >= r || y < 0 || y >= c ? false : true;
}
}