from itertools import * from bisect import * from collections import * from heapq import * import sys sys.setrecursionlimit(10 ** 6) def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def SI(): return sys.stdin.readline()[:-1] def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] int1 = lambda x: int(x) - 1 def MI1(): return map(int1, sys.stdin.readline().split()) def LI1(): return list(map(int1, sys.stdin.readline().split())) p2D = lambda x: print(*x, sep="\n") dij = [(1, 0), (0, 1), (-1, 0), (0, -1)] class Dinic(): def __init__(self, n, s, t): self.n, self.s, self.t = n, s, t self.to = defaultdict(list) self.level = [-1] self.max_flow = -1 def add_edge(self, u, v, cap): u_index_in_to_v = len(self.to[v]) v_index_in_to_u = len(self.to[u]) self.to[u].append([v, cap, u_index_in_to_v]) self.to[v].append([u, 0, v_index_in_to_u]) # 無向辺の追加 def add_undir_edge(self, u, v, cap): u_index_in_to_v = len(self.to[v]) v_index_in_to_u = len(self.to[u]) self.to[u].append([v, cap, u_index_in_to_v]) self.to[v].append([u, cap, v_index_in_to_u]) def set_level(self): s = self.s level = [-1] * self.n level[s] = 0 q = deque() q.append([s, 0]) while q: u, u_level = q.popleft() for v, cap, _ in self.to[u]: if cap == 0: continue if level[v] != -1: continue level[v] = u_level + 1 if v == self.t: self.level = level return True q.append([v, u_level + 1]) return False def dfs(self, u=-1, flow_to_me=10 ** 16): if u == -1: u = self.s if u == self.t: return flow_to_me flow_from_me = 0 u_level = self.level[u] for utov_i, (v, cap, vtou_i) in enumerate(self.to[u]): if self.level[v] != u_level + 1: continue if cap == 0: continue flow_to_v = self.dfs(v, min(cap, flow_to_me - flow_from_me)) if not flow_to_v: continue flow_from_me += flow_to_v self.to[u][utov_i][1] -= flow_to_v self.to[v][vtou_i][1] += flow_to_v return flow_from_me def calculation(self): res = 0 while self.set_level(): res += self.dfs() return res # これが出力用 def get_max_flow(self): if self.max_flow == -1: self.max_flow = self.calculation() return self.max_flow def main(): h,w=MI() ss=[SI() for _ in range(h)] aa=[[0]*w for _ in range(h)] # 始点を頂点0、終点を頂点1とするので、W,Bに2から頂点番号を振っていく cnt=2 for i,j in product(range(h),range(w)): if ss[i][j]==".":continue aa[i][j]=cnt cnt+=1 # 最大流のライブラリ mf=Dinic(cnt,0,1) # それぞれのマスから右と下に辺を張るとともにw,bの数を数える wc=bc=0 for i,j in product(range(h),range(w)): a=aa[i][j] if a==0:continue # 黒のとき(2部グラフの右側) if (i+j)%2: bc+=1 mf.add_edge(a,1,1) if j+1