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 Scala (judge verdict: Accepted)

import scala.io.StdIn

object Main extends App {

  val Array(h, w) = StdIn.readLine().split(' ').map(_.toInt)

  val tiles = (0 until h).map(_ => StdIn.readLine().split(' ')).toArray
  val square = Array.fill[Int](h + 1, w + 1)(0)
  var max = 0

  (1 to h).foreach { i =>
    (1 to w).foreach { k =>
      square(i)(k) = if(tiles(i-1)(k-1) == "0"){
        val t1 = square(i)(k-1)
        val t2 = square(i-1)(k-1)
        val t3 = square(i-1)(k)

        (if(t1 < t2){
          if(t1 < t3) t1 else t3
        } else if(t2 < t3){
          t2
        } else {
          t3
        }) + 1
      } else {
        0
      }

      max = scala.math.max(square(i)(k), max)
    } 
  }

  println(max*max)

}