#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
char map[105][105];
int b[105][105];
int m,n;
int s[4][2]={-1,0,1,0,0,-1,0,1};
struct node{
int x,y;
int step;
};
int cheak(int x,int y)
{
if(x>=1&&x<=m&&y>=1&&y<=n&&b[x][y]==0&&map[x][y]!='*')
return 1;
return 0;
}
int BFS(int x,int y)
{
queue<node>a;
node now;
now.x=x;
now.y=y;
now.step=0;
a.push(now);
while(!a.empty())
{
now=a.front();
a.pop();
if(map[now.x][now.y]=='C')
return now.step;
for(int i=0;i<4;i++)
{
int dx=now.x+s[i][0];
int dy=now.y+s[i][1];
if(cheak(dx,dy))
{
node next;
next.x=dx;
next.y=dy;
b[next.x][next.y]=1;
next.step=now.step+1;
a.push(next);
}
}
}
}
int main()
{
int k;
while(cin>>m>>n)
{
for(int i=1;i<=m;i++)
{
cin>>(map[i]+1);
}
memset(b,0,sizeof(b));
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(map[i][j]=='B')
{
b[i][j]=1;
k=BFS(i,j);
cout<<k<<endl;
}
}
}
}