#include using namespace std; const int N = 110; int n, m; char a[N][N]; //地图 int cnt; //目前的水坑数 //八个方向 int dx[] = {0, 0, -1, 1, -1, 1, -1, 1}; int dy[] = {1, -1, 0, 0, -1, -1, 1, 1}; void dfs(int x, int y) { //修改为.,防止再次更新 a[x][y] = '.'; //尝试八个方向 for (int k = 0; k < 8; k++) { int x1 = x + dx[k], y1 = y + dy[k];//目标点坐标 if (x1 >= 1 && x1 <= n && y1 >= 1 && y1 <= m && a[x1][y1] == 'W') dfs(x1, y1); } } int main() { //输入 cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) //发现水坑 if (a[i][j] == 'W') { dfs(i, j); //这个cnt++妙的很 cnt++; } cout << cnt << endl; return 0; }