15行秒杀
#include <bits/stdc++.h>
using namespace std;
#define N 105
char block[N][N];
int vis[N][N];
int n, m, k;
int main(){
cin >> n >> m;
for(int i = 0; i < n; i ++) cin >> block[i];
for (int j = 0; j < m; j ++){
if(block[n - 1][j] == 'B') k++;
while(block[n - 1 ][j] == 'B') j++;
}
cout << k << endl;
}
DFS && BFS
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int n, m;
char g[N][N];
bool st[N][N];
vector<PII> points[N];
int k;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0 , -1};
void bfs(int x, int y, vector<PII>& points)
{
queue<PII> q;
q.push({x, y});
st[x][y] = true;
while (!q.empty())
{
auto t = q.front(); q.pop();
points.push_back(t);
for (int i = 0; i < 4; i ++)
{
int a = t.first + dx[i], b = t.second + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (g[a][b] == '.') continue;
if (st[a][b]) continue;
st[a][b] = true;
q.push({a, b});
}
}
}
void dfs(int x, int y, vector<PII>& points)
{
points.push_back({x, y});
st[x][y] = true;
for (int i = 0; i < 4; i ++)
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (g[a][b] == '.') continue;
if (st[a][b]) continue;
dfs(a, b, points);
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++) cin >> g[i];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
if (g[i][j] == 'B' && !st[i][j])
bfs(i, j, points[k ++]);
cout << k;
}