#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;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnR5cGVkZWYgbG9uZyBsb25nIGxsOwp1c2luZyBuYW1lc3BhY2Ugc3RkOwp2b2lkIENvZGVfQnlfTW9oYW1lZF9LaGFsZWQoKSB7CiAgICBpb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gY2luPj5uPj5tOwogICAgdi5yZXNpemUobik7CiAgICBmb3IgKGF1dG8gJml0OnYpY2luPj5pdDsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj5pZHgobix2ZWN0b3I8aW50PihtLC0xKSk7CiAgICBmb3IgKGludCBpPTA7aTxuO2krKyl7CiAgICAgICAgZm9yIChpbnQgaj0wO2o8bTtqKyspewogICAgICAgICAgICBpZih2W2ldW2pdPT0nVycpewogICAgICAgICAgICAgICAgdy5wdXNoX2JhY2soe2ksan0pOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UgaWYodltpXVtqXT09J0cnKXsKICAgICAgICAgICAgICAgIGlkeFtpXVtqXT1nLnNpemUoKTsKICAgICAgICAgICAgICAgIGcucHVzaF9iYWNrKHtpLCBqfSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBhZGoucmVzaXplKHcuc2l6ZSgpKTsKICAgIGZvciAoaW50IGk9MDtpPHcuc2l6ZSgpO2krKyl7CiAgICAgICAgaW50IHg9d1tpXS5maXJzdCx5PXdbaV0uc2Vjb25kOwogICAgICAgIGZvciAoaW50IGQgPSAwOyBkIDw0O2QrKyl7CiAgICAgICAgICAgIGludCBpaT0geCArIGR4W2RdLGpqPXkrZHlbZF07CiAgICAgICAgICAgIGlmKGlpPj0wIGFuZCBpaTxuIGFuZCBqaj49MCBhbmQgamo8bSl7CiAgICAgICAgICAgICAgICBpZih2W2lpXVtqal09PSdHJyl7CiAgICAgICAgICAgICAgICAgICAgaW50IGlkPWlkeFtpaV1bampdOwogICAgICAgICAgICAgICAgICAgIGFkaltpXS5wdXNoX2JhY2soaWQpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgbWF0Y2guYXNzaWduKGcuc2l6ZSgpLCAtMSk7CiAgICBsbCBjbnQ9MDsKICAgIGZvciAoaW50IGk9MDtpPHcuc2l6ZSgpO2krKyl7CiAgICAgICAgdmlzLmFzc2lnbihnLnNpemUoKSxmYWxzZSk7CiAgICAgICAgaWYoZGZzKGkpKWNudCsrOwogICAgfQogICAgY291dDw8Y250OwogICAgcmV0dXJuIDA7Cn0=