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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
using System.Text;//テキストの高速出力に必要
class Program
{
	static void Main()
	{
		string[] input = Console.ReadLine().Split(' ');
		int h = int.Parse(input[0]);
		int w = int.Parse(input[1]);
    string[] strings = new string[h];
    for(int i = 0; i < h; i++) strings[i] = Console.ReadLine();
    long[,] matrix = new long[h-1,w-1];//行列
      
    for(int i = 0; i < h-1; i++)//それぞれの行
    {
    	for(int j = 0; j < w-1; j++)
      {
        int countMemo = 0;
        if(strings[i][j] == '#') countMemo++;
        if(strings[i][j+1] == '#') countMemo++;
        if(strings[i+1][j] == '#') countMemo++;
        if(strings[i+1][j+1] == '#') countMemo++;
        if(countMemo % 2 == 0) matrix[i,j] = 1;
      }
    }

		Console.WriteLine(LargestRectangleArea(matrix));
	}

  static long LargestRectangleArea(long[,] uses)
  {//長方形上で1の書かれたマスだけを用いた長方形の最大面積をO(HW)で返す
    long answer = 0;
    int h = uses.GetLength(0);
    int w = uses.GetLength(1);
    long[] heights = new long[w];
    for(int i = 0; i < h; i++)
    {
      for(int j = 0; j < w; j++)
      {
        if(uses[i,j] == 1) heights[j]++;
        else heights[j] = 0;
      }
      answer = Math.Max(answer,LargestRectangle(heights));
    }

    return answer;
  }

  static long LargestRectangle(long[] lengths)
  {//x軸に接する底辺1、高さlengthsの長方形を繋げた時、最大長方形の面積をO(n)で返す
    if(lengths.Length == 0) return 0;
    long answer = lengths[0]+1;
    int n = lengths.Length;
    var rectangleStack = new Stack<long[]>();//x座標と高さ
    rectangleStack.Push(new long[2]{0,lengths[0]});
    long[] topMemo = new long[2];
    
    for(int i = 1; i < n; i++)
    {
      if(rectangleStack.Count() > 0) topMemo = rectangleStack.Peek();
      
      if(topMemo[1] < lengths[i]) rectangleStack.Push(new long[2]{i,lengths[i]});
      else if(topMemo[1] > lengths[i])
      {
        long memo = 0;
        while(rectangleStack.Count() > 0)
        {
          if(rectangleStack.Peek()[1] > lengths[i])
          {
            answer = Math.Max(answer, (rectangleStack.Peek()[1]+1)*(i+1-rectangleStack.Peek()[0]));
            memo = rectangleStack.Peek()[0];
            
            if(rectangleStack.Count() == 1)
            {
              rectangleStack.Pop();
              rectangleStack.Push(new long[2]{memo, lengths[i]});
              break;
            }
            else rectangleStack.Pop();
          }else
          {
            rectangleStack.Push(new long[2]{memo, lengths[i]});
            break;
          }
        }
      }

      if(i == n-1 && rectangleStack.Count() > 0)
      {
        while(rectangleStack.Count() > 0)
        {
          answer = Math.Max(answer, (rectangleStack.Peek()[1]+1)*(i-rectangleStack.Peek()[0]+2));
          rectangleStack.Pop();
        }
      }
    }

    return answer;
  }
  
}