#include #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i,n) for(int i=0;i<(int)n;++i) #define each(a, b) for(auto (a): (b)) #define all(v) (v).begin(),(v).end() #define fi first #define se second #define pb push_back #define show(x) cout <<#x<<" = "<<(x)<P; const int MAX_N = 2502; char c[52][52]; vector

od,ev; vector kind1,kind2; struct edge { int to,cap,rev; }; vector G[MAX_N]; int level[MAX_N]; int iter[MAX_N]; void add_edge(int from,int to,int cap) { G[from].push_back((edge){to,cap,G[to].size()}); G[to].push_back((edge){from,0,G[from].size()-1}); } void bfs(int s) { memset(level,-1,sizeof(level)); queue que; level[s] = 0; que.push(s); while(!que.empty()){ int v = que.front(); que.pop(); rep(i,G[v].size()){ edge &e = G[v][i]; if(e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v,int t,int f) { if(v==t){ return f; } for(int &i = iter[v];i 0 && level[v] < level[e.to]){ int d = dfs(e.to,t,min(f,e.cap)); if(d>0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s,int t) { int flow = 0; for(;;){ bfs(s); if(level[t]<0){ return flow; } memset(iter,0,sizeof(iter)); int f; while((f=dfs(s,t,INF)) > 0){ flow += f; } } } int main() { int n,m; cin >> n >> m; int wnum = 0,bnum = 0; rep(i,n){ rep(j,m){ cin >> c[i][j]; if(c[i][j] == 'w'){ if((i+j)%2){ od.pb(P(i,j)); kind1.pb(0); }else{ ev.pb(P(i,j)); kind2.pb(0); } wnum++; }else if(c[i][j] == 'b'){ if((i+j)%2){ od.pb(P(i,j)); kind1.pb(1); }else{ ev.pb(P(i,j)); kind2.pb(1); } bnum++; } } } int l = (int)od.size(); int r = (int)ev.size(); rep(i,l){ add_edge(0,i+1,1); } rep(i,l){ rep(j,r){ P p1 = od[i]; P p2 = ev[j]; if(kind1[i] != kind2[j]){ if(abs(p1.fi-p2.fi) + abs(p1.se-p2.se) == 1){ add_edge(i+1,l+j+1,1); } } } } rep(i,r){ add_edge(l+i+1,l+r+1,1); } int f = max_flow(0,l+r+1); cout << abs(bnum-wnum) + 10 * (min(wnum,bnum) - f) + 100 * f << endl; return 0; }