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)

#include <stdio.h>
#define max(p,q)((p)>(q)?(p):(q))

int h[1510];
int tate[1510],yoko[1510],cnt;

int f(int n){
	cnt=1;
	int ans=0;
	for(int i=0;i<=n;i++){
		if(h[i]>tate[cnt-1]){
			tate[cnt]=h[i];
			yoko[cnt]=i;
			cnt++;
		}
		if(h[i]<tate[cnt-1]){
			while(tate[cnt-1]>h[i]){
				ans=max(ans,tate[cnt-1]*(i-yoko[cnt-1]));
				cnt--;
			}
			tate[cnt]=h[i];
			cnt++;
		}
	}
	return ans;
}

int main(){
	int hh,w;
	scanf("%d%d",&hh,&w);
	int ans=0;
	for(int i=0;i<hh;i++){
		int t;
		for(int j=0;j<w;j++){
			scanf("%d",&t);
			h[j]++;
			if(t)h[j]=0;
		}
		int temp=f(w);
		ans=max(ans,temp);
	}
	printf("%d\n",ans);
}