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)

#include<stdio.h>

int min(int x, int y, int z)
{
  int w;
  w=x;
  if(w>=y) w=y;
  if(w>=z) w=z;
  return w;
}

int main(void)
{
    int i,j,H,W,S=0;
    int dp[2][1400];
	int a[1400][1400];

    scanf("%d%d",&H,&W);
    for(i=0;i<=H-1;i++)
	{
		for(j=0;j<=W-1;j++)
		{
	       scanf("%d",&a[i][j]);
		}
    }
    for(i=0;i<=W-1;i++)
    {
        if(a[0][i]==0)
	    {
	        dp[0][i]=1;
	    }
        else
	    {
	        dp[0][i]=0;
	    }
        if(S<dp[0][i])
	    {
	        S=dp[0][i];
	    }
    }
    for(i=1;i<=H-1;i++)
    {
        for(j=0;j<=W-1;j++)
	    {
			if(j==0)
			{
				if(a[i][j]==0)
				{
					dp[1][j]=1;
				}
				else
				{
					dp[1][j]=0;
				}
			}
			else
			{
	            if(a[i][j]==0)
	            {
	               if(dp[0][j-1]==dp[0][j] && dp[0][j-1]==dp[1][j-1])
	               {
	                  dp[1][j]=dp[0][j-1]+1;
	               }
	               else
	               {
	                  dp[1][j]=min(dp[0][j-1],dp[0][j],dp[1][j-1])+1;
	               }
	               
	            }
	            else
	            {
	               dp[1][j]=0;
	            }
			}
			if(S<dp[1][j])
	        {
	            S=dp[1][j];
	        }
	    }
		for(j=0;j<=W-1;j++)
		{
			dp[0][j]=dp[1][j];
		}
    }
  printf("%d\n",S*S);
  return 0;
}