def min(a,b); a < b ? a : b; end def max(a,b); a > b ? a : b; end N = gets.to_i B = N.times.map { gets.chomp } W = Array.new(N, 0) Q = [] (0 ... N).each do |i| (i ... N).each do |j| if B[i][j] == 'o' W[i] += 1 elsif B[i][j] == 'x' W[j] += 1 elsif B[i][j] == '-' Q << [i, j] end end end def dfs(i) if i < Q.size a,b = Q[i] Q[i].map do |j| W[j] += 1 s = dfs(i + 1) W[j] -= 1 s end.min else r = (0 ... N).group_by{|i| W[i] } return r.keys.sort.reverse.index{|t| r[t].include?(0) } + 1 end end puts dfs(0)