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

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let h: usize = it.next().unwrap().parse().unwrap();
    let w: usize = it.next().unwrap().parse().unwrap();
    let mut a = vec![vec![0usize; w + 1]; h + 1];
    for a in a.iter_mut().take(h) {
        for a in a.iter_mut().take(w) {
            *a = it.next().unwrap().parse().unwrap();
        }
    }
    for i in (0..h).rev() {
        for j in 0..w {
            if a[i][j] == 1 {
                a[i][j] = 0;
            } else {
                a[i][j] = a[i + 1][j] + 1;
            }
        }
    }
    let mut ans = 0;
    for a in a.iter().take(h) {
        let mut stack: Vec<(usize, usize)> = vec![];
        for (i, &a) in a.iter().enumerate() {
            let mut k = i;
            while let Some(&(l, b)) = stack.last() {
                if a >= b {
                    break;
                } else {
                    ans = std::cmp::max(ans, (i - l) * b);
                    k = l;
                    stack.pop();
                }
            }
            stack.push((k, a));
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}