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)

// AOJ DPL_3_A Largest Square
// 2018.5.4 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif
int in()
{
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

short dp[1405][1405];

int largestSquare(int H, int W, char map[1405][1405])
{
	int r, c, t, ans;
	
	ans = 0;
	for (r = 1; r <= H; r++) for (c = 1; c <= W; c++) {
		if (map[r][c]) dp[r][c] = 0;
		else {
			t = dp[r-1][c-1];
			if (t > 0 && dp[r  ][c-1] < t) t = dp[r  ][c-1];
			if (t > 0 && dp[r-1][c  ] < t) t = dp[r-1][c  ];
			dp[r][c] = ++t;
			if (t > ans) ans = t;
		}
	}
	return ans*ans;
}

char map[1405][1405];
int main()
{
	int H, W, r, c;

	H = in(), W = in();
	for (r = 1; r <= H; r++) for (c = 1; c <= W; c++) {
		map[r][c] = gc() & 0xf, gc();
	}
	printf("%d\n", largestSquare(H, W, map));
	return 0;
}