Problem:
Largest Square

Given a matrix (H × W) which contains only 1 and 0, find the area of the largest square matrix 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 square.

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

4

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AizuOnline
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var n = Console.ReadLine().Split().Select(i => int.Parse(i)).ToArray();
            var H = n[0]; var W = n[1];

            var hw = new int[H, W];
            foreach (var h in Enumerable.Range(0, H))
            {
                var m = Console.ReadLine().Split().Select(i => int.Parse(i)).ToArray();
                for (int w = 0; w < m.Length; w++)
                {
                    hw[h, w] = m[w];
                }
            }

            var dp = new int[H, W];
            var max = int.MinValue;
            for (int i = 0; i < H; i++)
            {
                for (int j = 0; j < W; j++)
                {
                    if (hw[i, j] == 1)
                    {
                        dp[i, j] = 0;
                    }
                    else
                    {
                        if (i == 0 || j == 0)
                        {
                            dp[i, j] = 1;
                        }
                        else
                        {
                            dp[i, j] = Math.Min(dp[i, j - 1],
                                Math.Min(dp[i - 1, j - 1], dp[i - 1, j])) + 1;
                        }
                    }
                    max = Math.Max(dp[i, j], max);
                }
            }

            //var max = int.MinValue;
            //for (int i = 0; i < H; i++)
            //{
            //    for (int j = 0; j < W; j++)
            //    {
            //        max = Math.Max(dp[i, j], max);
            //    }
            //}

            Console.WriteLine(max * max);
            Console.ReadLine();
        }
    }
}