#include<iostream>
#include<queue>
using namespace std;
struct node {
int x,y,rsum;
};
int n,m,sx,sy,fx,fy;
char mp[310][310];
int xx[4] = {-1,1,0,0},yy[4] = {0,0,-1,1};
bool vis[310][310];
queue <node> Q;
node f1;
node f2;
void go_another(int a,int b) {
for(int i=1 ; i<=n ; i++)
for(int j=1 ; j<=m ; j++) {
if(mp[i][j]==mp[a][b]&&i!=a&&j!=b) {
f2.x = i;
f2.y = j;
f2.rsum = f1.rsum+1;
return ;
}
}
}
int main() {
cin >> n >> m;
int step = 1;
for(int i=1 ; i<=n ; i++)
for(int j=1 ; j<=m ; j++) {
char c;
cin >> c;
if(c=='@')
sx = i,sy = j;
else if(c=='=')
fx = i,fy = j;
mp[i][j] = c;
}
vis[1][1] = 1;
f1.x = sx,f1.y = sy,f1.rsum = 0;
Q.push(f1);
while(!Q.empty()) {
f1 = Q.front();
int dx,dy;
for(int i=0 ; i<4 ; i++) {
dx = f1.x+xx[i],dy = f1.y+yy[i];
if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&!vis[dx][dy]&&mp[dx][dy]!='#') {
if(mp[dx][dy]>='A'&&mp[dx][dy]<='Z') {
go_another(dx,dy);
vis[dx][dy] = 1;
Q.push(f2);
} else {
f2.x = dx;
f2.y = dy;
f2.rsum = f1.rsum+1;
vis[dx][dy] = 1;
Q.push(f2);
if(dx == fx&&dy == fy) {
cout << f2.rsum;
return 0;
}
}
}
}
Q.pop();
}
return 0;
}