N = gets.to_i S = N.times.map{ gets[0,N] } res = N.times.map{[nil]*N} S.each_with_index{|s, i| s.chars.each_with_index{|c, ii| res[i][ii] = c } } N.times{|x| if res[0][x] == ?- res[0][x] = ?o res[x][0] = ?x end } def f(res) if !res.any?{|a|a.any?{|c|c == ?-}} win = res[0].count(?o) return 1 + res.map{|a|a.count(?o)}.sort_by{|o|-o}.uniq.index(win) end N.times.map{|y| N.times.map{|x| next 9e9 if res[y][x] != ?- res[y][x] = ?o res[x][y] = ?x f(res).tap{ res[y][x] = ?- res[x][y] = ?- } }.min }.min end p f(res)