Problem:
Score : 700 points

Problem Statement

We have a board with an H \times W grid.
Each square in the grid is painted in black or white. The square at the i-th row from the top and j-th column from the left is black if the j-th character in S_i is #, and white if that character is ..

Snuke can perform the following operation on the grid any number of times:

Select a row or column in the grid, and invert the color of all the squares in that row or column (that is, black squares become white and vice versa).

Then, Snuke draws a rectangle along grid lines. Here, all the squares contained in the rectangle must be painted in black.

Find the maximum possible area of Snuke's rectangle when the operation is performed optimally.

Constraints

2 \leq H \leq 2000

2 \leq W \leq 2000

|S_i| = W

S_i consists of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
S_2
:
S_H

Output

Print the maximum possible area of Snuke's rectangle.

Sample Input 1

3 3
..#
##.
.#.

Sample Output 1

6

If the first row from the top and the third column from the left are inverted, a 2 \times 3 rectangle can be drawn, as shown below:

Sample Input 2

4 4
....
....
....
....

Sample Output 2

16

Sample Input 3

10 8
##...#.#
##...#.#
..###.#.
#.##.#.#
.#..#.#.
..##.#.#
##.#.#..
...#.#..
###.#.##
###..###

Sample Output 3

27

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

use dbg::{BooleanTable, Tabular};
use proconio::input;
use std::cmp::Ordering;
// dbg {{{
#[allow(dead_code)]
mod dbg {
    use std::fmt::{Debug, Formatter};

    #[derive(Clone)]
    pub struct Tabular<'a, T: Debug>(pub &'a [Vec<T>]);
    impl<'a, T: Debug> Debug for Tabular<'a, T> {
        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
            for i in 0..self.0.len() {
                writeln!(f, "{:2} | {:?}", i, &self.0[i])?;
            }
            Ok(())
        }
    }

    #[derive(Clone)]
    pub struct BooleanTable<'a>(pub &'a [Vec<bool>]);
    impl<'a> Debug for BooleanTable<'a> {
        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
            for i in 0..self.0.len() {
                writeln!(f, "{:2} | {:?}", i, BooleanSlice(&self.0[i]))?;
            }
            Ok(())
        }
    }

    #[derive(Clone)]
    pub struct BooleanSlice<'a>(pub &'a [bool]);
    impl<'a> Debug for BooleanSlice<'a> {
        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
            write!(
                f,
                "{}",
                self.0
                    .iter()
                    .map(|&b| if b { '#' } else { '.' })
                    .collect::<String>()
            )?;
            Ok(())
        }
    }
}
// }}}

fn maximal_rectangles(a: &[usize]) -> Vec<(usize, usize, usize)> {
    let mut ans = Vec::new();
    let mut stack = Vec::<(usize, usize)>::new();
    for (x1, y1) in std::iter::once(0)
        .chain(a.iter().copied().chain(std::iter::once(0)))
        .enumerate()
    {
        let mut x = x1;
        while let Some((x0, y0)) = stack.pop() {
            match y0.cmp(&y1) {
                Ordering::Less => {
                    stack.push((x0, y0));
                    stack.push((x, y1));
                    break;
                }
                Ordering::Equal => {
                    stack.push((x0, y0));
                    break;
                }
                Ordering::Greater => {
                    ans.push((x0, x1, y0));
                }
            }
            x = x0;
        }
        if stack.is_empty() {
            stack.push((x1, y1));
        }
    }
    ans
}

fn max_area(a: &[usize]) -> Option<usize> {
    maximal_rectangles(a)
        .iter()
        .map(|(start, end, y)| (end - start + 1) * y)
        .max()
}

fn main() {
    input!(h: usize, w: usize, s: [proconio::marker::Chars; h]);
    let mut g = vec![vec![false; w - 1]; h - 1];
    for i in 0..h - 1 {
        for j in 0..w - 1 {
            g[i][j] = [s[i][j], s[i][j + 1], s[i + 1][j], s[i + 1][j + 1]]
                .iter()
                .copied()
                .map(|c| c as u8 as u32)
                .sum::<u32>()
                % 2
                == 1;
        }
    }
    let mut a = vec![vec![1; w - 1]; h - 1];
    for i in 0..h - 1 {
        for j in 0..w - 1 {
            if !g[i][j] {
                a[i][j] = i.checked_sub(1).map(|im1| a[im1][j] + 1).unwrap_or(2);
            }
        }
    }

    println!(
        "{}",
        a.iter()
            .map(|v| max_area(v.as_slice()).unwrap_or(0))
            .max()
            .unwrap()
    );
}