#include<bits/stdc++.h>
using namespace std;
int W,H,sx,sy,ex,ey,xxx,yyy,fz[4][2] = {{-1,0},{0,-1},{1,0},{0,1}},ans;
char a[101][101];
bool f;
struct node{
int x,y,ls,fxx,fxy;
};
queue<node> q;
void bfs()
{
q.push({sx,sy,0,0,0});
a[sx][sy] = '*';
while(!q.empty())
{
node head = q.front();
q.pop();
for(int i = 0;i < 4;i++)
{
xxx = head.x + fz[i][0];
yyy = head.y + fz[i][1];
if(xxx > 0 && xxx <= H && yyy > 0 && yyy <= W && a[xxx][yyy] != '*')
{
if((head.fxx == 0 && head.fxy == 0)|| (head.fxx == fz[i][0] && head.fxy == fz[i][1])) q.push({xxx,yyy,head.ls,fz[i][0],fz[i][1]});
else q.push({xxx,yyy,head.ls + 1,fz[i][0],fz[i][1]});
a[xxx][yyy] = '*';
if(xxx == ex && yyy == ey)
{
ans = head.ls;
return;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> W >> H;
for(int i = 1;i <= H;i++) for(int j = 1;j <= W;j++)
{
cin >> a[i][j];
if(a[i][j] == 'C')
{
if(!f) sx = i,sy = j;
else ex = i,ey = j;
}
}
bfs();
cout << ans;
return 0;
}