#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 22;
int n, m;
char g[N][N];
int have[200];
int ans = 1;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int x, int y, int cnt)
{
ans = max(ans, cnt);
for(int i=0; i<4; i++) {
int xx = dx[i] + x;
int yy = dy[i] + y;
if(xx>=0 && xx<n && yy>=0 && yy<m) {
if(have[g[xx][yy]] == 0) {
have[g[xx][yy]] = 1;
dfs(xx, yy, cnt+1);
have[g[xx][yy]] = 0;
}
}
}
}
int main()
{
cin >> n >> m;
for(int i=0; i<n; i++)
scanf("%s", g[i]);
have[g[0][0]] = 1;
dfs(0, 0, 1);
cout << ans << endl;
return 0;
}