def dfs g, v, used, match used[v]=true g[v].each{|u| w=match[u] if !w or (!used[w] and dfs(g, w, used, match)) match[v]=u match[u]=v return true end } if g[v] false end def bipartite_matching g res=0 match={} g.keys.each{|v| unless match[v] used={} res+=1 if dfs(g, v, used, match) end } res end h, w=gets.split.map &:to_i w+=2 choco=?.*w h.times { choco<=choco.size or choco[i+j]!~/b|w/ graph[i] ? graph[i]<< i+j : graph[i]=[i+j] end end a=bipartite_matching graph b=[black, white].min-a c=black+white-2*(a+b) puts 100*a+10*b+c