#include using namespace std; #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n)-1;i>=0;i--) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define printV(v) for(auto&& x : v){cout << x << " ";} cout << endl #define printVV(vv) for(auto&& v : vv){for(auto&& x : v){cout << x << " ";}cout << endl;} #define printP(p) cout << p.first << " " << p.second << endl #define printVP(vp) for(auto&& p : vp) printP(p); typedef long long ll; typedef pair Pii; typedef tuple TUPLE; typedef vector vi; typedef vector vvi; typedef vector vvvi; typedef vector vp; const int inf = 1e9; const int mod = 1e9 + 7; template using Graph = vector>; struct edge { int to, cap, rev; }; const int MAX_V = 2510; vector G[MAX_V]; int level[MAX_V]; int iter[MAX_V]; void add_edge(int from, int to, int cap) { G[from].emplace_back((edge){to, cap, (int)G[to].size()}); G[to].emplace_back((edge){from, 0, (int)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(); for (int i = 0; i < G[v].size(); i++) { 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 < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 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 H, W; // N, E, S, W const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = { 0, 1, 0, -1}; bool inside(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; } int node(int i, int j) { return i * W + j; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> H >> W; vector choco(H); rep(i, H) cin >> choco[i]; const int s = H * W, t = s + 1; int nb = 0, nw = 0; // s -> odd -> even -> t rep(i, H) { rep(j, W) { if (choco[i][j] == 'b') { nb++; } else if (choco[i][j] == 'w') { nw++; } if ((i + j) % 2) { add_edge(s, node(i, j), 1); rep(k, 4) { int ni = i + dx[k], nj = j + dy[k]; if (inside(ni, nj) && ((choco[i][j] == 'b' && choco[ni][nj] == 'w') || (choco[i][j] == 'w' && choco[ni][nj] == 'b'))) { add_edge(node(i, j), node(ni, nj), 1); } } } else { add_edge(node(i, j), t, 1); } } } int match = max_flow(s, t); int ans = 100 * match; nb -= match, nw -= match; ans += 10 * min(nb, nw); ans += max(nb - nw, nw - nb); cout << ans << endl; }