#include <bits/stdc++.h>
using namespace std;
int n,m,sx,sy,fx,fy,ans;
int d[4][2]={
{0,-1},
{0,1},
{1,0},
{-1,0},
};
char mp[305][305],sc,tc;
bool vis[305][305];
struct qwp {int x,y,t;};
queue<qwp> q;
void go_to(){
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
tc=mp[i][j];
if(tc==sc and i!=fx and j!=fy){
fx=i;
fy=j;
return;
}
}
}
}
int bfs(){
int x,y,t;
q.push({sx,sy,0});
while(!q.empty()){
x=q.front().x;
y=q.front().y;
t=q.front().t;
q.pop();
if(mp[x][y]=='=') return t;
for(int i=0;i<4;i++){
fx=x+d[i][0];
fy=y+d[i][1];
if(fx<0 or fy<0 or fx>=n or fy>=m or mp[fx][fy]=='#' or vis[fx][fy])
continue;
vis[fx][fy]=true;
if(mp[fx][fy]>='A' and mp[fx][fy]<='Z'){
sc=mp[fx][fy];
go_to();
}
q.push({fx,fy,t+1});
}
}
}
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='@') sx=i,sy=j;
}
ans=bfs();
printf("%d",ans);
return 0;
}
写的有点丑,别介意