# Here your code ! V = [] G = Hash.new {|h,k| h[k] = [] } def add_virt(color) v = V.size V << color v end def add_edge(u,v) G[u] << v G[v] << u end def dfs(u, matches, used = Array.new(V.size)) used[u] = true G[u].find do |v| w = matches[v] if !w || (!used[w] && dfs(w, matches, used)) matches[u] = v matches[v] = u true end end end def matching matches = Array.new(V.size) V.size.times.inject(0) do |s, v| s += 1 if !matches[v] && dfs(v, matches) s end end n, m = gets.split.map(&:to_i) map = n.times.map do gets.chomp end vmap = Array.new(n) { Array.new(m,nil) } n.times do |y| m.times do |x| if map[y][x] == 'w' vmap[y][x] = add_virt(:w) elsif map[y][x] == 'b' vmap[y][x] = add_virt(:b) end end end n.times do |y| m.times.each_cons(2) do |a,b| if vmap[y][a] && vmap[y][b] add_edge(vmap[y][a], vmap[y][b]) end end end m.times do |x| n.times.each_cons(2) do |a,b| if vmap[a][x] && vmap[b][x] add_edge(vmap[a][x], vmap[b][x]) end end end a,b = [:w,:b].map{|color| V.count(color) }.sort c = matching puts c * 100 + (a - c) * 10 + (b - a)