#include<bits/stdc++.h>
using namespace std;
int n,m,dx[]={0,1,0,-1},dy[]={1,0,-1,0};
char a[305][305];
struct node{
int xx,yy,step;
}st;
queue<node>q;
node nx;
void gotoxy(char x){
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(a[i][j]==x){
nx.xx=i;
nx.yy=j;
//nx.step-=1;
return;
}
}
}
}
int bfs(){
while(!q.empty()){
st=q.front();
q.pop();
for(int i=0;i<4;++i){
nx.xx=st.xx+dx[i];
nx.yy=st.yy+dy[i];
nx.step=st.step+1;
if(a[nx.xx][nx.yy]=='=')
return nx.step;
if(a[nx.xx][nx.yy]>='A' && a[nx.xx][nx.yy]<='Z')
gotoxy(a[nx.xx][nx.yy]);
if(a[nx.xx][nx.yy]=='#') continue;
q.push(nx);
a[nx.xx][nx.yy]='#';
}
}
}
int main(){
memset(a,'#',sizeof(a));
int i,j;
cin>>n>>m;
for(i=1;i<=n;++i){
for(int j=1;j<=m;++j){
cin>>a[i][j];
if(a[i][j]=='@'){
st.step=0;
st.xx=i;
st.yy=j;
}
}
}
a[st.xx][st.yy]='#';
q.push(st);
cout<<bfs()<<endl;
return 0;
}