#include<bits/stdc++.h>
using namespace std;
const int MAXN = 507;
//vector <int> a[MAXN];
int n, m, cnt, ans;
bool mark[MAXN][MAXN];
char a[MAXN][MAXN];
queue <pair<int, int>> q;
vector <pair <int, int>> save;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};

bool check(int x, int y){return(x >= 1 and y >= 1 and y <= m and x <= n and !mark[x][y]);}

void bfs(int i, int j){
    mark[i][j] = 1;
    q.push({i, j});
    int cnt = 1;
    bool kt = false;
    while(!q.empty()){
        auto [x, y] = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int u = x + dx[i];
            int v = y + dy[i];
            if(check(u, v) and a[u][v] == '.')  kt = 1;
            if(check(u, v) and a[u][v] == 'W'){
                mark[u][v] = 1;
                cnt++;
                q.push({u, v});
            }
        }
    }
    if(!kt) ans += cnt;
}

int main(){
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
            if(a[i][j] == 'W') save.push_back({i, j});
        }
    }
    for(auto [x, y] : save) if(!mark[x][y])bfs(x, y);
    cout << ans;
}