//I tried dfs on grid and memoized the solution but this is getting
//wrong somewhere. Can you help please.
// problem : spoj/ABCPATH/
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
int n,m,x,y,rr;
char a[55][55];
int dp[55][55];
int dx[] = {-1,-1,0,1,1,1,0,-1};
int dy[] = {0,1,1,1,0,-1,-1,-1};
bool isValid(int fx, int fy, char cp)
{
cp++;
if(fx<0||fx>=n||fy<0||fy>=m) return false;
if(a[fx][fy]!=cp) return false;
return true;
}
int dfs(int sx, int sy)
{
if(dp[sx][sy]!=-1) return dp[sx][sy];
int pp=1;
int df = 0;
for(int i=0; i<8; i++)
{
int nx = sx+dx[i];
int ny = sy+dy[i];
if(isValid(nx,ny,a[sx][sy]))
{
int rp = dfs(nx,ny);
df = max(df,rp);
}
}
pp+=df;
return dp[sx][sy] = pp;
}
int main () {
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
// freopen("input.txt", "r", stdin);
int pp=1;
while(1)
{
cin>>n>>m;
if(n==0 && m==0) break;
memset(dp,-1,sizeof(dp));
vector<pair<int,int> > p;
p.clear();
int ans=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
cin>>a[i][j];
if(a[i][j]=='A') p.push_back({i,j});
}
}
if(p.size()==0)
{
cout<<ans<<nl;
continue;
}
for(int i=0; i<p.size(); i++)
{
rr = dfs(p[i].first,p[i].second);
ans = max(ans,rr);
}
cout<<"Case "<<pp++<<": "<<ans<<nl;
}
return 0;
}
Ly9JIHRyaWVkIGRmcyBvbiBncmlkIGFuZCBtZW1vaXplZCB0aGUgc29sdXRpb24gYnV0IHRoaXMgaXMgZ2V0dGluZwovL3dyb25nIHNvbWV3aGVyZS4gQ2FuIHlvdSBoZWxwIHBsZWFzZS4KCi8vIHByb2JsZW0gOiBzcG9qL0FCQ1BBVEgvCgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBubCAiXG4iCgppbnQg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