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

let solve h w c =
  let v0 = Array.make w 0 in
  let v1 = Array.make w 0 in
  let r = ref 0 in
  Array.iteri (fun i _ -> if c.(0).(i) = 0 then begin v0.(i) <- 1; r := 1 end) v0;
  for i = 1 to h - 1 do
    if c.(i).(0) = 0 then v1.(0) <- 1;
    for j = 1 to w - 1 do
      if c.(i).(j) = 1 then v1.(j) <- 0
      else begin
        let t = if v0.(j) < v1.(j-1) then v0.(j) else v1.(j-1) in
        v1.(j) <- if v0.(j-1) < t then v0.(j-1) + 1 else t + 1;
        r := if v1.(j) > !r then v1.(j) else !r;
      end
    done;
    Array.iteri (fun i e -> v0.(i) <- e) v1
  done;
  !r * !r

let () =
  let (h, w) = Scanf.scanf "%d %d\n" (fun h w -> (h, w)) in
  let c = Array.make_matrix h w 0 in
  let rec read = function
    | (i, _) when i = h -> ()
    | (i, j) when j = w -> read (i + 1, 0)
    | (i, j) ->
      c.(i).(j) <- Scanf.scanf "%d " (fun i -> i);
      read (i, j + 1) in
  read (0, 0);
  Printf.printf "%d\n" (solve h w c)