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 C++14 (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int H, W;
int fld[1500][1500];
int dp[1500][1500];

int main() {
    cin >> H >> W;
    rep(i, H) rep(j, W) cin >> fld[i][j];

    rep(i, W) dp[0][i] = (fld[0][i] + 1) % 2;
    rep(i, H) dp[i][0] = (fld[i][0] + 1) % 2;

    for (int i = 1; i < H; i++) {
        for (int j = 1; j < W; j++) {
            if (fld[i][j] == 1) dp[i][j] = 0;
            else {
                int t;
                t = min(dp[i-1][j-1], dp[i-1][j]);
                t = min(t, dp[i][j-1]);
                dp[i][j] = t + 1;
            }
        }
    }

    int ans = 0;
    rep(i, H) rep(j, W) chmax(ans, dp[i][j]);
    cout << ans*ans << endl;
}