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.Linq;

//最大正方形
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_3_A
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 5");
            WillReturn.Add("0 0 1 0 0");
            WillReturn.Add("1 0 0 0 0");
            WillReturn.Add("0 0 0 1 0");
            WillReturn.Add("0 0 0 1 0");
            //4
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int UB_X;
    static int UB_Y;

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();
        SplitAct(InputList[0]);

        int H = wkArr[0];
        int W = wkArr[1];

        UB_X = W - 1;
        UB_Y = H - 1;
        int[,] BanArr = new int[UB_X + 1, UB_Y + 1];

        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            SplitAct(InputList[LoopY + 1]);
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                BanArr[LoopX, LoopY] = wkArr[LoopX];
            }
        }

        int[,] CntArr = new int[UB_X + 1, UB_Y + 1];

        Func<int, int, int> wkFunc = (pX, pY) =>
        {
            if (pX < 0 || UB_X < pX) return 0;
            if (pY < 0 || UB_Y < pY) return 0;

            return CntArr[pX, pY];
        };

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr[LoopX, LoopY] == 1) //汚れたタイルの場合
                    CntArr[LoopX, LoopY] = 0;
                else {
                    int Cnt1 = wkFunc(LoopX - 1, LoopY - 1);
                    int Cnt2 = wkFunc(LoopX, LoopY - 1);
                    int Cnt3 = wkFunc(LoopX - 1, LoopY);

                    int wkMin = Math.Min(Cnt1, Cnt2);
                    wkMin = Math.Min(wkMin, Cnt3);

                    CntArr[LoopX, LoopY] = wkMin + 1;
                }
            }
        }

        int MaxSeihoukeiLength = CntArr.Cast<int>().Max();
        Console.WriteLine(MaxSeihoukeiLength * MaxSeihoukeiLength);
    }
}