#include<iostream>
#include<cstdio>
using namespace std;
char a[21][21];
bool b[26];
int X[4]={0,1,0,-1}, Y[4]={1,0,-1,0};
int m=0;
int R,C;
void DFS(int r,int c,int s)
{
if (s m)
m=s;
for (int i=0;i<4;i++)
{
int x=r+X[i];
int y=c+Y[i];
if (x>=1&&x<=R&&y>=1&&y<=C&&!b[a[x][y]-'A'])
{
b[a[x][y]-'A']=1;
DFS(x, y, s+1);
b[a[x][y]-'A']=0;
}
}
}
int main()
{
cin>>R>>C;
for (int i=1;i<=R;i++)
{
for (int j=1;j<=C j++)
{
cin>>a[i][j];
}
}
b[a[1][1]-'A']=1;
DFS(1,1,1);
cout<<m<<endl;
return 0;
}
//老师的(我不会)