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: Accepted)

module IO = struct

  (* @since 4.04.0 *)
  let split_on_char sep s =
    let open String in
    let r = ref [] in
    let j = ref (length s) in
    for i = length s - 1 downto 0 do
      if get s i = sep then begin
        r := sub s (i + 1) (!j - i - 1) :: !r;
        j := i
      end
    done;
    sub s 0 !j :: !r

  let read_ns () = read_line () |> split_on_char ' ' |> List.map int_of_string

end

let solve h w c =
  let s = Array.make w 0 in
  let t = Array.make w 0 in
  let r = ref 0 in
  List.iteri (fun j e -> if e = 0 then begin
    s.(j) <- 1;
    r := 1;
  end) c.(0);
  if !r = 0 then begin
    let rec doit i =
      if i = h then ()
      else begin
        match c.(i) with
        | [] -> ()
        | hd :: _ ->
          if hd = 0 then r := 1
          else doit (i + 1)
      end in
    doit 0
  end;
  Array.iteri (fun i l ->
    if i = 0 then ()
    else begin
      match l with
      | [] -> assert false
      | x :: xs ->
        if x = 0 then t.(0) <- 1;
        List.iteri (fun j e ->
          let j = j + 1 in
          if e = 1 then t.(j) <- 0
          else begin
            let d = if s.(j) < t.(j-1) then s.(j) else t.(j-1) in
            t.(j) <- 1 + if s.(j-1) < d then s.(j-1) else d;
            if t.(j) > !r then r := t.(j)
          end) xs;
        Array.iteri (fun i e -> s.(i) <- e) t
    end)
    c;
  !r * !r

let () =
  match IO.read_ns () with
  | [h; w] ->
    Array.init h (fun _ -> IO.read_ns ()) |> solve h w |> Printf.printf "%d\n"
  | _ -> assert false