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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    struct Rectangle
    {
        internal int height, pos;
    }

    class Program
    {
        static void Main(string[] args)
        {
            int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            int H = input[0];
            int W = input[1];

            int[,] dp = new int[H, W + 1];

            for (int i = 0; i < H; i++)
            {
                input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

                for (int j = 0; j < W; j++)
                {
                    if (input[j] == 1) dp[i, j] = 0;
                    else dp[i, j] = i > 0 ? dp[i - 1, j] + 1 : 1;
                }
            }

            Stack<Rectangle> stk = new Stack<Rectangle>();
            int maxArea = 0;

            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j <= W; j++)
                {
                    Rectangle rect;
                    rect.height = dp[i, j];
                    rect.pos = j;

                    if (stk.Count == 0)
                    {
                        stk.Push(rect);
                    }
                    else
                    {
                        if (stk.Peek().height < rect.height)
                        {
                            stk.Push(rect);
                        }
                        else if (stk.Peek().height > rect.height)
                        {
                            int target = j;

                            while (stk.Count > 0 && stk.Peek().height >= rect.height)
                            {
                                Rectangle pre = stk.Pop();
                                int area = pre.height * (j - pre.pos);
                                maxArea = Math.Max(maxArea, area);
                                target = pre.pos;
                            }
                            rect.pos = target;
                            stk.Push(rect);
                        }
                    }
                }
            }

            Console.WriteLine(maxArea);
        }
    }
}