#include<bits/stdc++.h>
using namespace std;
const int inf=INT_MAX;
int w,h,startx,starty,endx,endy,turn[105][105],dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
bool readstart;
char mp[105][105],dir[4]={'l','r','u','d'};
struct P{
int x,y,step;
};
queue<P>q;
bool check(int x,int y,int step) {
if(x<1||y<1||x>h||y>w||mp[x][y]=='*'||turn[x][y]<step) {
return false;
}
return true;
}
void dfs(char dir,P p) {
if(!check(p.x,p.y,p.step)) {
return;
}
int x=p.x,y=p.y,s=p.step;
turn[x][y]=s;
switch (dir) {
case 'l':
q.push({x,y,s});
dfs('l',{x,y-1,s});
break;
case 'r':
q.push({x,y,s});
dfs('r',{x,y+1,s});
break;
case 'u':
q.push({x,y,s});
dfs('u',{x-1,y,s});
break;
case 'd':
q.push({x,y,s});
dfs('d',{x+1,y,s});
break;
}
}
void bfs(int stx,int sty,int enx,int eny) {
q.push({stx,sty,-1});
while(!q.empty()) {
P now=q.front();
for(int i=0;i<4;i++) {
P nx;
nx.x=now.x+dx[i],nx.y=now.y+dy[i];
dfs(dir[i],{nx.x,nx.y,nx.step+1});
}
}
}
int main() {
cin>>w>>h;
for(int i=1;i<=h;i++) {
for(int j=1;j<=w;j++) {
cin>>mp[i][j];
if(mp[i][j]=='C') {
if(readstart==false) {
startx=i;
starty=j;
readstart=true;
}
else {
endx=i;
endy=j;
}
}
if(mp[i][j]=='*') {
turn[i][j]=inf;
}
}
}
bfs(startx,starty,endx,endy);
cout<<turn[endx][endy];
return 0;
}