#include<bits/stdc++.h>
#define ac in-'A'+1
using namespace std;
struct pt{
int x,y,tx,ty;
}door[30];
queue<int> x,y;
char mp[320][320];
bool bdr[30],flag,bmp[320][320];
int n,m,fx,fy,t=0,K,SZ,X,Y;
int main(){
char in;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>in;
if(in=='='){
fy=i;
fx=j;
}
else if(in=='@'){
y.push(i);
x.push(j);
}
else if(in=='#')mp[i][j]='#';
else if(in!='.'){
if(!bdr[ac]){
door[ac].x=i;
door[ac].x=j;
bdr[ac]=1;
}
else{
door[ac].tx=i;
door[ac].tx=j;
}
mp[i][j]=in;
}
else{
mp[i][j]='.';
}
}
}
cout<<fx<<" "<<fy<<endl;
while(!flag){
SZ=x.size();
K=1;
//cout<<"SZ"<<SZ<<endl;
while(K<=SZ){
if(!bmp[y.front()][x.front()]&&1<=y.front()<=n&&1<=x.front()<=m&&mp[y.front()][x.front()]!='#'){
bmp[y.front()][x.front()]=1;
X=x.front();
Y=y.front();
//cout<<Y<<" "<<X<<endl;
if(y.front()==fy&&x.front()==fx){
flag=1;
break;
}
if('A'<=mp[y.front()][x.front()]<='Z'){
if(door[mp[y.front()][x.front()]-'A'+1].tx==x.front()&&door[mp[y.front()][x.front()]-'A'+1].ty==y.front()){
x.push(door[mp[y.front()][x.front()]-'A'+1].x);
y.push(door[mp[y.front()][x.front()]-'A'+1].y);
}
else{
x.push(door[mp[y.front()][x.front()]-'A'+1].tx);
y.push(door[mp[y.front()][x.front()]-'A'+1].ty);
}
}
x.push(X+1);
y.push(Y);
//cout<<Y<<" "<<X+1<<endl;
x.push(X-1);
y.push(Y);
//cout<<Y<<" "<<X-1<<endl;
x.push(X);
y.push(Y+1);
//cout<<Y+1<<" "<<X<<endl;
x.push(X);
y.push(Y-1);
//cout<<Y-1<<" "<<X<<endl;
//cout<<endl;
}
//cout<<x.front()<<" "<<y.front()<<endl;
x.pop();
y.pop();
//cout<<x.front()<<" "<<y.front()<<endl;
K++;
}
if(!flag)t++;
}
printf("%d",t);
return 0;
}
/*5 5
###=##
###A##
###.##
@....
###A#
*/