<Problem>
https://oj.vnoi.info/problem/vmunch
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int n, m;
int res = 0;
char ch[100][100];
bool mark[100][100]{ false };
int dx[4]{ 0, 0, -1, 1 };
int dy[4]{ -1, 1, 0, 0 };
class Node {
public:
int x, y, t;
public:
Node(int x, int y, int t = 0) {
this->x = x;
this->y = y;
this->t = t;
}
};
void dfs(int x, int y) {
queue<Node> iDex;
iDex.push(Node(x, y));
mark[x][y] = true;
while (!iDex.empty()) {
Node d = iDex.front();
iDex.pop();
if (d.x == 0 && d.y == 0) {
res = max(res, d.t);
}
else {
for (int i = 0; i < 4; i++) {
int nextX = d.x + dx[i];
int nextY = d.y + dy[i];
if ((nextX >= 0 && nextX < n) && (nextY >= 0 && nextY < m)
&& !mark[nextX][nextY] && (ch[nextX][nextY] == '.' || ch[nextX][nextY] == 'B')) {
iDex.push(Node(nextX, nextY, d.t + 1));
mark[nextX][nextY] = true;
}
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
strcpy(ch[i], s.c_str());
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (ch[i][j] == 'C') {
dfs(i, j);
cout << res << endl;
return 0;
}
}
}
return 0;
}