

package simple_test;
import java.util.*;
public class Main {
static int r;
static int c;
static char[][] board;
static int[] dx = { 1, 0, -1, 0 };
static int[] dy = { 0, 1, 0, -1 };
static boolean[] v;
static int max;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(sc.nextLine(), " ");
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
v = new boolean[26];
board = new char[r][c];
for (int i = 0; i < r; i++) {
board[i] = sc.nextLine().toCharArray();
}
BackT(0, 0, 0);
System.out.println(max);
}
public static void BackT(int x, int y, int cnt) {
if (v[board[x][y] - 'A']) {
max = Math.max(max, cnt);
return;
}
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < r && 0 <= ny && ny < c) {
v[board[x][y] - 'A'] = true;
BackT(nx, ny, ++cnt);
v[board[x][y] - 'A'] = false;
--cnt;
}
}
}
}