#include<iostream>
#include<queue>
using namespace std;
const int N = 1e3 + 5;
int n, m, res;
char ch[N][N];
int st[N][N], x[8] = {0,0,1,-1,-1,-1,1,1}, y[8] = {1,-1,0,0,1,-1,1,-1};
struct node{
int x, y;
};
void bfs(int dx, int dy){
queue<node> q;
q.push({dx, dy});
st[dx][dy] = 1;
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = 0; i < 8; i++){
int nx = now.x + x[i], ny = now.y + y[i];
if(nx < 0 || nx >= n || ny < 0 || ny >= m || st[nx][ny] || ch[nx][ny] == '.') continue;
st[nx][ny] = 1;
q.push({nx, ny});
}
}
res++;
}
int main(){
cin >> n >> m;
for(int i = 0; i < n; i++) scanf("%s", &ch[i]);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(ch[i][j] == 'W' && !st[i][j]){
bfs(i, j);
}
}
}
cout << res << endl;
return 0;
}