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

mod utils {
    use std::error::Error;
    use std::io::stdin;
    use std::str::FromStr;

    #[allow(dead_code)]
    pub fn read_line<T>() -> Result<Vec<T>, Box<Error>>
    where
        T: FromStr,
        T::Err: 'static + Error,
    {
        let mut line = String::new();
        let _ = stdin().read_line(&mut line)?;
        let parsed_line = line.split_whitespace()
            .map(|x| x.parse::<T>())
            .collect::<Result<Vec<T>, T::Err>>()?;

        Ok(parsed_line)
    }

    #[allow(dead_code)]
    pub fn read_lines<T>(n: usize) -> Result<Vec<Vec<T>>, Box<Error>>
    where
        T: FromStr,
        T::Err: 'static + Error,
    {
        (0..n).map(|_| read_line()).collect()
    }
}

use std::cmp::min;
fn solve() -> Result<(), Box<std::error::Error>> {
    let header = utils::read_line::<usize>()?;
    let height = header[0];
    let width = header[1];
    let matrix = utils::read_lines::<usize>(height)?;

    let mut dp = matrix
        .iter()
        .map(|row| row.iter().map(|x| 1 - x).collect::<Vec<_>>())
        .collect::<Vec<_>>();

    for i in 1..height {
        for j in 1..width {
            if dp[i][j] != 0 {
                let minimum_neighbors = min(min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]);
                dp[i][j] = minimum_neighbors + 1;
            }
        }
    }

    let l = dp.iter()
        .map(|row| row.iter().max().unwrap())
        .max()
        .unwrap();

    // println!("{:?}", dp);
    println!("{}", l * l);

    Ok(())
}

fn main() {
    match solve() {
        Err(err) => panic!("{}", err),
        _ => (),
    };
}