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 JAVA (judge verdict: Wrong Answer)

import java.util.*;

class Main{
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int h = scan.nextInt();
		int w = scan.nextInt();
		scan.nextLine();
		int[][] arr = new int[h][w];
		for(int i = 0; i < h; i++) {
			String[] line = scan.nextLine().split(" ");
			for(int j = 0; j < w; j++) {
				arr[i][j] = Integer.parseInt(line[j]);
			}
		}
		int[][] ansArr = new int[h][w];
		int ans = 0;
		for(int i = 0; i < h; i++) ansArr[i][0] = (arr[i][0]==0)?1:0;
		for(int i = 0; i < w; i++) ansArr[0][i] = (arr[0][i]==0)?1:0;
		for(int i = 1; i < h; i++) {
			for(int j = 1; j < w; j++) {
				ansArr[i][j] = (arr[i][j]==0)?Math.min(Math.min(ansArr[i-1][j],ansArr[i][j-1]),ansArr[i-1][j-1])+1:0;
			}
		}
		for(int[] _arr : ansArr) for(int num : _arr) ans = Math.max(ans,num);
		System.out.println(ans);
	}
}