uses math;
const maxn = 100;
maxq = 100001;
oo = 10000000;
fi = '';
fo = '';
type tqueue = record
front, rear, nitem: longint;
ix: array[0..maxq-1] of shortint;
iy: array[0..maxq-1] of shortint;
end;
var m,n:byte;
sx,sy,fx,fy: shortint;
a,dp: array[0..maxn+1,0..maxn+1] of byte;
queue: tqueue;
procedure enter;
var i,j:byte;
p: char;
begin
assign(input,fi);
reset(input);
readln(m,n);
for i:=1 to m do
begin
for j:=1 to n do
begin
read(p);
case p of
'B':
begin
sx:=i;
sy:=j;
a[i,j]:=1;
end;
'C':
begin
fx:=i;
fy:=j;
a[i,j]:=1;
end;
'*': a[i,j]:=0;
'.': a[i,j]:=1;
end;
end;
readln;
end;
close(input);
end;
procedure init;
begin
with queue do
begin
front:=0;
rear:=1;
nitem:=0;
end;
end;
procedure push(x,y:shortint);
begin
with queue do
begin
inc(nitem);
front:=(front+1) mod maxq;
ix[front]:=x;
iy[front]:=y;
end;
end;
procedure pop(var x,y:shortint);
begin
with queue do
begin
dec(nitem);
x:=ix[rear];
y:=iy[rear];
rear:=(rear+1) mod maxq;
end;
end;
function empty:boolean;
begin
with queue do
empty:=(nitem=0);
end;
function kc(x1,y1,x2,y2:shortint):byte;
begin
kc:=abs(x1-x2)+abs(y1-y2);
end;
procedure printa;
var i,j:byte;
begin
for i:=1 to m do
begin
for j:=1 to n do
write(dp[i,j]:3,' ');
writeln;
end;
writeln;
end;
procedure solve;
var x,y,i,j:byte;
d: array[0..maxn+1,0..maxn+1] of longint;
free: array[0..maxn+1,0..maxn+1] of boolean;
minkc:byte;
procedure printa;
var i,j:byte;
begin
for i:=1 to m do
begin
for j:=1 to n do
write(d[i,j]:15,' ');
writeln;
end;
writeln;
end;
begin
assign(output,fo);
rewrite(output);
for i:=1 to m do
for j:=1 to n do
d[i,j]:=oo;
d[sx,sy]:=0;
fillchar(free,sizeof(free),true);
repeat
x:=0;
y:=0;
for i:=1 to m do
for j:=1 to n do
if free[i,j] and ((d[i,j]<d[x,y]) or ((x=0) and (y=0))) and (a[i,j]=1) then
begin
x:=i;
y:=j;
end;
free[x,y]:=false;
if ((x=0) and (y=0)) or ((x=fx) and (y=fy)) then break;
if (x>1) and (a[x-1,y]=1) and (d[x,y]+1<d[x-1,y]) then d[x-1,y]:=d[x,y]+1;
if (x<m) and (a[x+1,y]=1) and (d[x,y]+1<d[x+1,y]) then d[x+1,y]:=d[x,y]+1;
if (y>1) and (a[x,y-1]=1) and (d[x,y]+1<d[x,y-1]) then d[x,y-1]:=d[x,y]+1;
if (y<n) and (a[x,y+1]=1) and (d[x,y]+1<d[x,y+1]) then d[x,y+1]:=d[x,y]+1;
until false;
writeln(d[fx,fy]);
close(output);
end;
begin
enter;
solve;
end.
dXNlcyBtYXRoOwpjb25zdCBtYXhuID0gMTAwOwogICAgICBtYXhxID0gMTAwMDAxOwogICAgICBvbyA9IDEwMDAwMDAwOwogICAgICBmaSA9ICcnOwogICAgICBmbyA9ICcnOwp0eXBlIHRxdWV1ZSA9IHJlY29yZAogICAgICAgICAgICAgICAgZnJvbnQsIHJlYXIsIG5pdGVtOiBsb25naW50OwogICAgICAgICAgICAgICAgaXg6IGFycmF5WzAuLm1heHEtMV0gb2Ygc2hvcnRpbnQ7CiAgICAgICAgICAgICAgICBpeTogYXJyYXlbMC4ubWF4cS0xXSBvZiBzaG9ydGludDsKICAgICAgICAgICAgICBlbmQ7CnZhciBtLG46Ynl0ZTsKICAgIHN4LHN5LGZ4LGZ5OiBzaG9ydGludDsKICAgIGEsZHA6IGFycmF5WzAuLm1heG4rMSwwLi5tYXhuKzFdIG9mIGJ5dGU7CiAgICBxdWV1ZTogdHF1ZXVlOwpwcm9jZWR1cmUgZW50ZXI7CnZhciBpLGo6Ynl0ZTsKICAgIHA6IGNoYXI7CmJlZ2luCiAgYXNzaWduKGlucHV0LGZpKTsKICByZXNldChpbnB1dCk7CiAgcmVhZGxuKG0sbik7CiAgZm9yIGk6PTEgdG8gbSBkbwogIGJlZ2luCiAgICBmb3Igajo9MSB0by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