(h,w),*x=$<.map{_1.split.map &:to_i} a=h*w b=Hash.new(h+1) x.map{b[_2]-=x=[b[_2]-_1,0].max;p a-=x}