v:array[1..4] of longint=(0,1,-1,0);
var i,j,n,m,head,tail:longint;
a:array[1..100,1..100] of char;
k1,k2,l1,l2:array['A'..'Z'] of longint;
f:array[1..100,1..100] of longint;
b:array[1..10000,1..2] of longint;
procedure search(x,y:longint);
var x1,y1,u1,v1,k3,k4,l3,l4:longint;
begin
head:=0; tail:=0; a[x,y]:='1';
inc(tail); b[tail,1]:=x; b[tail,2]:=y;
inc(head);
while head<=tail do
begin
x1:=b[head,1]; y1:=b[head,2];
for j:=1 to 4 do
begin
u1:=x1+u[j]; v1:=y1+v[j];
if (u1>=1) and (u1<=n) and (v1>=1) and (v1<=m) then
if (a[u1,v1]<>'1') and (((f[u1,v1]<>0) and (f[u1,v1]>f[x1,y1]+1)) or
(f[u1,v1]=0)) then
begin
inc(tail);
b[tail,1]:=u1;
b[tail,2]:=v1;
f[u1,v1]:=f[x1,y1]+1;
if (a[u1,v1]>='A') and (a[u1,v1]<='Z') then
begin
k3:=k1[a[u1,v1]]; k4:=k2[a[u1,v1]];
l3:=l1[a[u1,v1]]; l4:=l2[a[u1,v1]];
if k3=u1 then
begin
inc(tail);
b[tail,1]:=l3;
b[tail,2]:=l4;
if (f[l3,l4]>f[u1,v1]) or (f[l3,l4]=0) then f[l3,l4]:=f[u1,v1];
end
else begin
inc(tail);
b[tail,1]:=k3;
b[tail,2]:=k4;
if (f[k3,k4]>f[u1,v1]) or (f[k3,k4]=0) then f[k3,k4]:=f[u1,v1];
end;
end;
end;
end;
inc(head);
end;
if f[n,m]=0 then writeln('No Solution.')
else writeln(f[n,m]);
end;
begin
readln(n,m);
for i:=1 to n do
begin
for j:=1 to m do
begin
read(a[i,j]);
if (a[i,j]>='A') and (a[i,j]<='Z') then
if k1[a[i,j]]=0 then
begin
k1[a[i,j]]:=i; k2[a[i,j]]:=j;
end
else begin
l1[a[i,j]]:=i; l2[a[i,j]]:=j;
end;
end;
readln;
end;
search(1,1);
end.