N, M = gets.split.map &:to_i R = $<.map{|s| s.split.map &:to_i } maximum = M.times.map{ [0, []]} counts = [0] * N ans = 0 R.each_with_index{|r, i| maximum = maximum.zip(r).map{|(max, idxs), x| case x <=> max when 1 idxs.each{|j| counts[j] -= 1 ans -= 1 if counts[j] < 1 } counts[i] += 1 ans += 1 [x, [i]] when -1 [max, idxs] else counts[i] += 1 ans += 1 [max, idxs + [i]] end } p ans }