#include <bits/stdc++.h> using namespace std; int n,m; int sx,sy,ex,ey,ans; char Map[301][301]; int book[301][301]; int nt[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int nx,ny; struct node{ int x,y,step; }; queue q; void change(int &a,int &b){ for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(Map[i][j]==Map[a][b] && (i!=a || j!=b)) {a=i,b=j;return;} } } } void bfs(int sx,int sy){ q.push(node{sx,sy,0}); book[sx][sy]=1; node ft; int nx,ny; while(!q.empty()){ ft = q.front(); if(ft.x==ex && ft.y==ey) {ans=ft.step;break;} if(Map[ft.x][ft.y]>='A' && Map[ft.x][ft.y]<='Z'){ change(ft.x,ft.y); } for(int i=0;i<4;i++){ nx=ft.x+nt[i][0]; ny=ft.y+nt[i][1]; if(nx>=1&&nx<=n&&ny>=1&&ny<=m && book[nx][ny]!=1 && Map[nx][ny]!='#'){ book[nx][ny] = 1; q.push(node{nx,ny,ft.step+1}); } } q.pop(); } } int main(){ cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) {cin>>Map[i][j]; if(Map[i][j]=='@') sx=i,sy=j; if(Map[i][j]=='=') ex=i,ey=j;} bfs(sx,sy); cout<<ans; return 0; }