Problem:
Largest Rectangle

Given a matrix (H × W) which contains only 1 and 0, find the area of the largest rectangle which only contains 0s.

Input

H W
c1,1 c1,2 ... c1,W
c2,1 c2,2 ... c2,W
:
cH,1 cH,2 ... cH,W

In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given.

Output

Print the area (the number of 0s) of the largest rectangle.

Constraints

1 ≤ H, W ≤ 1,400

Sample Input

4 5
0 0 1 0 0
1 0 0 0 0
0 0 0 1 0
0 0 0 1 0

Sample Output

6

Solution in Ruby (judge verdict: Wrong Answer)

height, width = gets.split.map &:to_i

c = []
(0...height).each do |i|
  c[i] = gets.split.map &:to_i
end

h = Array.new(height).map { Array.new(width) }

(0...height).each do |i|
  (0...width).each do |j|
    if i == 0
      h[i][j] = c[i][j] == 0 ? 1 : 0
    else
      if c[i][j] == 1
        h[i][j] = 0
      else
        h[i][j] = h[i-1][j] + 1
      end
    end
  end
end

stack = []
max = 0

class Rect
  attr_accessor :h, :pos

  def initialize(h, pos)
    @h, @pos = h, pos
  end
end

(0...height).each do |i|
  (0...width).each do |j|
    if stack.empty?
      stack.push Rect.new(h[i][j], j)
    else
      if stack[-1].h < h[i][j]
        stack.push Rect.new(h[i][j], j)
      elsif stack[-1].h > h[i][j]
        pos = j
        while !stack.empty? && stack[-1].h >= h[i][j]
          r = stack.pop
          area = r.h * (j - r.pos)
          max = max > area ? max : area
          pos = r.pos
        end
        stack.push Rect.new(h[i][j], pos)
      end
    end
  end
end

puts max