vy:array[1..4]of longint=(0,0,1,-1);
type code=record
x,y,time:longint;
end;
var
h,t,i,j,n,m,k,l,o,p:longint;
a:array[-100..100,-100..100]of char;
b:array[-100..100,-100..100]of boolean;
w:array['A'..'Z',0..1]of code;
s:array[0..10000]of code;
z:string;
procedure doit;
begin
h:=0;t:=1;
b[1,1]:=false;
s[1].x:=1;
s[1].y:=1;
s[1].time:=0;
repeat
inc(h);
for i:=1 to 4 do
begin
k:=s[h].x+vx[i];
l:=s[h].y+vy[i];
if b[k,l] then
begin
if a[k,l]='0' then
begin
b[k,l]:=false;
inc(t);
s[t].x:=k;
s[t].y:=l;
s[t].time:=s[h].time+1;
end
else
begin
if (w[a[k,l],0].x=k)and(w[a[k,l],0].y=l)and(b[w[a[k,l],1].x,w[a[k,l],1].y]) then
begin
o:=k;p:=l;
k:=w[a[o,p],1].x;
l:=w[a[o,p],1].y;
b[k,l]:=false;
inc(t);
s[t].x:=k;
s[t].y:=l;
s[t].time:=s[h].time+1;
end
else
if b[w[a[k,l],0].x,w[a[k,l],0].y] then
begin
o:=k;p:=l;
k:=w[a[o,p],0].x;
l:=w[a[o,p],0].y;
b[k,l]:=false;
inc(t);
s[t].x:=k;
s[t].y:=l;
s[t].time:=s[h].time+1;
end;
end;
end;
if (s[t].x=n)and(s[t].y=m) then
begin
write(s[t].time);
halt;
end;
end;
until h>t;
write('No Solution.');
end;
procedure init;
begin
readln(n,m);
for i:=1 to n do
begin
readln(z);
for j:=1 to m do
begin
a[i,j]:=z[j];
if a[i,j]='1' then continue;
if a[i,j]='0' then begin b[i,j]:=true;continue;end
else if (w[a[i,j],0].x=0)and(w[a[i,j],0].y=0) then
begin
w[a[i,j],0].x:=i;
w[a[i,j],0].y:=j;
b[i,j]:=true;
end
else
begin
w[a[i,j],1].x:=i;
w[a[i,j],1].y:=j;
b[i,j]:=true;
end;
end;
end;
end;
begin
fillchar(a,sizeof(a),0);
fillchar(s,sizeof(s),0);
fillchar(b,sizeof(b),false);
fillchar(w,sizeof(w),0);
init;
doit;
end.