#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
void Code_By_Mohamed_Khaled() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
}
ll mod=1e9+7;
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod;}
ll dx[]={-1,1,0,0};
ll dy[]={0,0,-1,1};
ll n,m;vector<string>v;
vector<pair<ll,ll>>w,g;
vector<vector<ll>>adj;
vector<ll>match;vector<bool>vis;
bool dfs(ll node) {
    for (auto it:adj[node]) {
        if (!vis[it]) {
            vis[it]=true;
            if (match[it]==-1 or dfs(match[it])) {
                match[it]=node;
                return true;
            }
        }
    }
    return false;
}
int main() {
    Code_By_Mohamed_Khaled();
    cin>>n>>m;
    v.resize(n);
    for (auto &it:v)cin>>it;
    vector<vector<int>>idx(n,vector<int>(m,-1));
    for (int i=0;i<n;i++){
        for (int j=0;j<m;j++){
            if(v[i][j]=='W'){
                w.push_back({i,j});
            }
            else if(v[i][j]=='G'){
                idx[i][j]=g.size();
                g.push_back({i, j});
            }
        }
    }
    adj.resize(w.size());
    for (int i=0;i<w.size();i++){
        int x=w[i].first,y=w[i].second;
        for (int d = 0; d <4;d++){
            int ii= x + dx[d],jj=y+dy[d];
            if(ii>=0 and ii<n and jj>=0 and jj<m){
                if(v[ii][jj]=='G'){
                    int id=idx[ii][jj];
                    adj[i].push_back(id);
                }
            }
        }
    }
    match.assign(g.size(), -1);
    ll cnt=0;
    for (int i=0;i<w.size();i++){
        vis.assign(g.size(),false);
        if(dfs(i))cnt++;
    }
    cout<<cnt;
    return 0;
}