#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct haha
{
int x,y,shu;
};
int mapp[105][105];
char s[105][105];
int dx[5]={0,1,0,-1,0};
int dy[5]={0,0,1,0,-1};
int a,b;
int zmx[30];
int zmy[30];
int zmxx[30];
int zmyy[30];
queue<haha>qu;
bool p=0;
void bfs()
{
int i;
haha ls;
ls.x=1;
ls.y=1;
ls.shu=0;
qu.push(ls);
while(!qu.empty())
{
int xx=qu.front().x,yy=qu.front().y;
int sshh=qu.front().shu;
if(mapp[xx][yy]<sshh)
{
qu.pop();
continue;
}
for(i=1;i<=4;i++)
{
int newx=xx+dx[i],newy=yy+dy[i];
if(newx>=1&&newx<=a&&newy>=1&&newy<=b&&sshh+1<mapp[newx][newy])
{
mapp[newx][newy]=sshh+1;
ls.x=newx;ls.y=newy;
ls.shu=sshh+1;
qu.push(ls);
}
}
if('A'<=s[xx][yy-1]&&s[xx][yy-1]<='Z')
{
int nxx=zmx[s[xx][yy-1]-'A'+1];
int nyy=zmy[s[xx][yy-1]-'A'+1];
int nnxx=zmxx[s[xx][yy-1]-'A'+1];
int nnyy=zmyy[s[xx][yy-1]-'A'+1];
if(xx==nxx&&yy==nyy)
{
if(sshh<mapp[nnxx][nnyy])
{
mapp[nnxx][nnyy]=sshh;
ls.x=nnxx;
ls.y=nnyy;
ls.shu=sshh;
qu.push(ls);
}
}
else
{
if(sshh<mapp[nxx][nyy])
{
mapp[nxx][nyy]=sshh;
ls.x=nxx;
ls.y=nyy;
ls.shu=sshh;
qu.push(ls);
}
}
}
if(mapp[a][b]<=10050)
{
cout<<mapp[a][b];
p=1;
return;
}
qu.pop();
}
}
int main()
{
freopen("moshou.in","r",stdin);
freopen("moshou.out","w",stdout);
int i,k;
cin>>a>>b;
//cout<<a;
memset(mapp,127,sizeof(mapp));
for(i=1;i<=a;i++)
{
//cout<<"*";
//cout<<"*";
scanf("%s",s[i]);
//puts(s[i]);
for(k=0;k<strlen(s[i]);k++)
{
if(s[i][k]=='1')
{
mapp[i][k+1]=-1;
}
if(s[i][k]>='A'&&s[i][k]<='Z')
{
if(zmx[s[i][k]-'A'+1]==0&&zmy[s[i][k]-'A'+1]==0)
{
zmx[s[i][k]-'A'+1]=i;
zmy[s[i][k]-'A'+1]=k+1;
}
else
{
zmxx[s[i][k]-'A'+1]=i;
zmyy[s[i][k]-'A'+1]=k+1;
}
}
}
}
//for(i=1;i<=a;i++)
//printf("%s\n",s[i]);
mapp[1][1]=0;
//cout<<"*";
bfs();
/*int j;
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
cout<<mapp[i][j]<<" ";
cout<<endl;
}*/
if(!p)
cout<<"No Solution.";
return 0;
}