#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
char g[1010][1010];
bool v[1010][1010];
int n, m, ans;
int dx[8]={1,0,-1,0,1,-1,1,-1},dy[8]={0,1,0,-1,1,-1,-1,1};
queue<PII> q;
void bfs(int x, int y) {
q.push({x, y}); v[x][y] = 1;
while (q.size()) {
auto x = q.front(); q.pop();
for (int i = 0; i < 8; i++) {
int a = x.first + dx[i], b = x.second + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (g[a][b] == '.' || v[a][b]) continue;
q.push({a, b});
v[a][b] = 1;
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
scanf("%s", g[i]);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] == 'W' && !v[i][j]) {
ans++; bfs(i, j);
}
cout << ans << endl;
return 0;
}