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
c
1,1
c
1,2
...
c
1,W
c
2,1
c
2,2
...
c
2,W
:
c
H,1
c
H,2
...
c
H,W
In the first line, two integers
H
and
W
separated by a space character are given. In the following
H
lines,
c
i
,
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

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std ;
#define pb push_back
#define ll long long 
const ll mod = 1e9+7;

bool check(ll x , ll n, ll m, vector<vector<ll>>&ps){
    for(ll i=0;i+x-1<n;i++){
        for(ll j=0;j+x-1<m;j++){
            ll x1 = i, y1 = j, x2 = x1+x-1, y2 = y1+x-1;
            ll sum = ps[x2][y2];
            if(y1)sum -= ps[x2][y1-1];
            if(x1)sum -= ps[x1-1][y2];
            if(x1 and y1) sum += ps[x1-1][y1-1];
            if(!sum)return 1;
        }
    }
    return 0;
}


void solve(){
    ll n,m;
    cin>>n>>m;

    vector<vector<ll>>arr(n,vector<ll>(m));
    vector<vector<ll>>ps(n,vector<ll>(m));


    for(ll i=0;i<n;i++){
        for(ll j=0;j<m;j++){
            cin>>arr[i][j];
            if(j)arr[i][j] += arr[i][j-1];
        }
    }

    for(ll i=0;i<n;i++){
        for(ll j=0;j<m;j++){
            ps[i][j] = arr[i][j];
            if(i) ps[i][j] += ps[i-1][j];
        }
    }


    ll lo = 1, hi = min(n,m), ans = 0;

    while(lo<=hi){
        ll mid = (lo+hi)/2;
        if(check(mid,n,m,ps)){
            ans = mid;
            lo = mid+1;
        }
        else {
            hi = mid-1;
        }
    }

    cout<<ans * ans <<endl;


    
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t =1;
    // cin>>t;
    while(t--)solve();
}
accuracy: 1
time_ms: 80
memory_kb: 33964
final_rank: 14

code_snippet: #include <cstdio>
#include <algorithm>
using namespace std;
const int maxv=1401;
int dp[maxv][maxv];
int c[maxv][maxv];
int H,W;
int maxsquare(){
    int maxwidth=0;
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++){
            if(c[i][j]){
                dp[i][j]=0;
            }else {
                dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
                maxwidth=max(maxwidth,dp[i][j]);
            }
        }
    }
    return maxwidth;
}
int main(){
    scanf("%d %d",&H,&W);
    for(int i=0;i<=H;i++){
        dp[i][0]=0;
    }
    for(int i=0;i<=W;i++){
        dp[0][i]=0;
    }
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++){
            int g;
            scanf("%d",&g);
            c[i][j]=g;
        }
    }
    int l=maxsquare();
    printf("%d\n",l*l);
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 18288
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
    int h, w;
    cin >> h >> w;
    int grid[h][w];
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> grid[i][j];
        }
    }

    int dp[h][w];

    int ans = 0;

    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            dp[i][j] = (grid[i][j] == 1 ? 0 : 1);
            ans |= dp[i][j];
        }
    }

    for (int j = 0; j < w; j++)
    {
        dp[0][j] = (grid[0][j] == 1 ? 0 : 1);
    }

    for (int i = 1; i < h; i++)
    {
        for (int j = 1; j < w; j++)
        {
            if (grid[i][j] == 1)
            {
                dp[i][j] = 0;
            }
            else
            {
                dp[i][j] = min( min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1] ) + 1;
                ans = max(ans, dp[i][j]);
            }
        }
    }

    cout << (ans*ans) << endl;

    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 18636
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main()
{
    int h, w;
    cin >> h >> w;
    int grid[h][w];
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> grid[i][j];
        }
    }

    int dp[h][w];

    int ans = (grid[0][0] == 1 ? 0 : 1);

    for (int i = 0; i < h; i++)
    {
        dp[i][0] = (grid[i][0] == 1 ? 0 : 1);
    }

    for (int j = 0; j < w; j++)
    {
        dp[0][j] = (grid[0][j] == 1 ? 0 : 1);
    }

    for (int i = 1; i < h; i++)
    {
        for (int j = 1; j < w; j++)
        {
            if (grid[i][j] == 1)
            {
                dp[i][j] = 0;
            }
            else
            {
                dp[i][j] = min( min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1] ) + 1;
                ans = max(ans, dp[i][j]);
            }
        }
    }

    cout << (ans*ans) << endl;

    return 0;
}
accuracy: 0.775
time_ms: 30
memory_kb: 6420
final_rank: 19

code_snippet: #include <iostream>
#include <cstdio>
using namespace std;

const int maxn = 1400 + 5;

int Getint(){
	char c = getchar();
	bool flag = false;
	int ans = 0;
	while(c != '-' && (c < '0' || c > '9'))c = getchar();
	if(c == '-')flag = true,c = getchar();
	while(c >= '0' && c <= '9')ans = ans*10 + c-'0',c = getchar();
	return (flag == true) ? - ans : ans;
}

int N,M;
int ch[maxn][maxn];
int f[maxn][maxn];

int DP(){
	int ans = 0;
	
	for(int i = 1;i <= N; i++){
		for(int j = 1;j <= M; j++){
			f[i][j] = (ch[i][j] + 1) % 2;
			ans |= f[i][j];
		}
	}
	
	for(int i = 1;i <= N; i++){
		for(int j = 1;j <= M; j++){
			if(ch[i][j] == 1)f[i][j] = 0;
			else ans = max(ans,f[i][j] = min(f[i-1][j],min(f[i][j-1],f[i-1][j-1]))+1);
		}
	}
	
	return ans*ans;
}

int main(){
	N = Getint();
	M = Getint();
	for(int i = 1;i <= N; i++){
		for(int j = 1;j <= M; j++){
			ch[i][j] = Getint();
		}
	}
	int ans = DP();
	printf("%d\n",ans);
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 18968
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
bool c[1405][1405];
int dp[1405][1405];
int main(){
	int h,w;
	cin>>h>>w;
	int i,j;
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			cin>>c[i][j];
			if(!c[i][j]){
				dp[i][j]=1;
			}
		}
	}
	int ans=0;
	for(i=1;i<=h;i++){
		for(j=1;j<=w;j++){
			if(!c[i][j]){
				dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
			}
			ans=max(ans,dp[i][j]);
		}
	}
	cout<<ans*ans<<endl;
}
accuracy: 1
time_ms: 150
memory_kb: 12796
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long 
#define endl '\n'
void solve(){
    int n,m;cin>>n>>m;
    int c[n+1][m+1];
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>c[i][j];
	int dp[n+1][m+1];
	for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) dp[i][j]=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
		 	if(c[i][j]) dp[i][j]=0;
		 	else if(dp[i-1][j]!=dp[i][j-1]) dp[i][j]=min(dp[i-1][j],dp[i][j-1])+1;
		 	else if(c[i-dp[i-1][j]][j-dp[i-1][j]]) dp[i][j]=dp[i-1][j];
		 	else dp[i][j]=dp[i-1][j]+1;
		}
   	}
   	int ans=0;
   	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ans=max(ans,dp[i][j]);
   	ans=ans*ans;
   	cout<<ans<<endl;
}
signed main(){
 	ios::sync_with_stdio(false);
 	cin.tie(0),cout.tie(0);
 	int t=1;
 	//cin>>t;
 	while(t--){
 		solve();
 	}
 	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 34120
final_rank: 13

code_snippet: #include<iostream>
#include<cmath>
using namespace std;
bool hw[1405][1405];
long long dp[1405][1405];
int main()
{
	int h;
	int w;
	cin>>h>>w;
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			cin>>hw[i][j];
		}
	}
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			if(hw[i][j]==0)
				dp[i][j]=1;
			else
				dp[i][j]=0;
		}
	}
	for(int i=2;i<=h;i++)
	{
		for(int j=2;j<=w;j++)
		{
			if(hw[i][j]==0)
				dp[i][j]=min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]))+1;
			else
				dp[i][j]=0;
		}
	}
	long long maxx=0;
	for(int i=1;i<=h;i++)
	{
		for(int j=1;j<=w;j++)
		{
			
				maxx=max(dp[i][j],maxx);
			
		}
	}
	cout<<pow(maxx,2)<<endl;
	return 0;
}
accuracy: 0.85
time_ms: 110
memory_kb: 20512
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[1400][1400],g[1400][1400];
int main(){
		int h,w;
		cin>>h>>w;
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++)
				cin>>g[i][j];
		int maxwidth=0;
		for(int i=1;i<=h;i++)
			for(int j=1;j<=w;j++){
				dp[i][j]=(g[i][j]==1)?0:min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]))+1;
				maxwidth=max(maxwidth,dp[i][j]);
			}
		cout<<maxwidth*maxwidth<<endl;
}
accuracy: 0.925
time_ms: 100
memory_kb: 17764
final_rank: 16

code_snippet: #include <iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
#define MAX 1400
int dp[MAX][MAX],G[MAX][MAX];
int getLargestSquare(int H,int W)
{
    int maxWidth=0;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            dp[i][j]=(G[i][j]+1)%2;
            maxWidth=dp[i][j];
        }
    }
    for(int i=1;i<H;i++){
        for(int j=1;j<W;j++){
            if(G[i][j]){
                dp[i][j]=0;
            }else{
            dp[i][j]=min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]))+1;
            maxWidth=max(maxWidth,dp[i][j]);
            }
        }
    }
    return maxWidth*maxWidth;
}
int main()
{
    int H,W;
    scanf("%d %d",&H,&W);
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            scanf("%d",&G[i][j]);
        }
    }
    printf("%d\n",getLargestSquare(H,W));
    return 0;
}
accuracy: 0.775
time_ms: 20
memory_kb: 13796
final_rank: 20

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 1400 + 10;

int row, column;
int maze[MAXN][MAXN], pre[MAXN][MAXN], dp[MAXN][MAXN];
int ans;

bool isVaild(int r0, int c0, int r1, int c1){
	return !(pre[r1][c1] - pre[r0 - 1][c1] - pre[r1][c0 - 1] + pre[r0 - 1][c0 - 1]);
}

int main(void)
{
	scanf("%d%d", &row, &column);
	for(int r = 1; r <= row; r++)	for(int c = 1; c <= column; c++)
		scanf("%d", &maze[r][c]), pre[r][c] = pre[r][c - 1] + pre[r - 1][c] + maze[r][c] - pre[r - 1][c - 1];
	for(int r = 1; r <= row; r++)	for(int c = 1; c <= column; c++)	for(int i = 0; i < min(r, c); i++){
		if(!isVaild(r - i, c - i, r, c))	continue;
		ans = max(ans, dp[r][c] = (i + 1) * (i + 1));
		//printf("dp[%d][%d] = %d\n", r, c, dp[r][c]);
	}printf("%d\n", ans);
	return 0;
}
accuracy: 1
time_ms: 790
memory_kb: 25748
final_rank: 15

code_snippet: #include <cstdio>
#include <algorithm>

constexpr int MAX = 1'400;

int dp[MAX][MAX], G[MAX][MAX];

int get_largest_square(int H, int W) {
    int max_width = 0;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            dp[i][j] = (G[i][j] + 1) % 2; // dp初期化　汚れたタイル(1):0　きれいなタイル(0):1　となるような計算
            max_width |= dp[i][j];
        }
    }

    for (int i = 1; i < H; ++i) {
        for (int j = 1; j < W; ++j) {
            if (G[i][j]) {
                // 汚れたタイル
                dp[i][j] = 0;
            } else {
                // きれいなタイル
                dp[i][j] = std::min(dp[i - 1][j - 1], std::min(dp[i - 1][j], dp[i][j - 1])) + 1;
                max_width = std::max(max_width, dp[i][j]);
            }
        }
    }

    return max_width * max_width;
}

int main() {
    int H, W;

    scanf("%d %d", &H, &W);

    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            scanf("%d", &G[i][j]);
        }
    }

    printf("%d\n", get_largest_square(H, W));

    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 18356
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <limits.h>
using namespace std;


int main() {
    int H;
    int W;
    cin >> H >> W;
    //int c[1400][1400];
    //int dp[1400][1400];
    vector<vector<int>> c(1400, vector<int>(1400, 0));
    vector<vector<int>> dp(1400, vector<int>(1400, 0));

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> c[i][j];
        }
    }

    int maxWidth = 0;
    // dpの初期化
    // きれいなタイルは1, 汚いタイルは0
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (c[i][j]) { // 汚いタイル
                dp[i][j] = 0;
            }
            else { // きれいなタイル
                dp[i][j] = 1;
                maxWidth |= dp[i][j];
            }
        }
    }

    // 動的計画法で正方形の最大の辺の長さを求める
    // i,jどちらも1からスタート
    // 左上に向かって正方形を大きくしていく想定
    // dpの左上, 上, 左に注目し、最小値に+1したものをdp[i][j]に入れる
    for (int i = 1; i < H; i++) {
        for (int j = 1; j < W; j++) {
            if (dp[i][j] != 0) { // dp = 0の時は無視(汚いタイルなため)
                int tmpMin = INT_MAX;
                // 左上
                if (tmpMin >= dp[i - 1][j - 1]) {
                    tmpMin = dp[i - 1][j - 1];
                }
                // 左
                if (tmpMin >= dp[i][j - 1]) {
                    tmpMin = dp[i][j - 1];
                }
                // 上
                if (tmpMin >= dp[i - 1][j]) {
                    tmpMin = dp[i - 1][j];
                }

                dp[i][j] = tmpMin + 1;

                if (maxWidth <= dp[i][j]) {
                    maxWidth = dp[i][j];
                }
            }
        }
    }

    cout << maxWidth * maxWidth << endl;
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 18428
final_rank: 12

code_snippet: #include<cstdio>
#include<algorithm>
using namespace std;
#define MAX 1400

int dp[MAX][MAX];
int G[MAX][MAX];

int getLargestSquare(int H, int W) {
    int maxWidth = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            dp[i][j] = (G[i][j]) & 2;
            maxWidth |= dp[i][j];
        }
    }

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (G[i][j] == 1) {
                dp[i][j] = 0;
            } else {
                dp[i][j] = min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1;
                maxWidth = max(maxWidth, dp[i][j]);
            }
        }
    }
    
    return maxWidth * maxWidth;
}

int main (void) {
    int H;
    int W;

    scanf("%d %d", &H, &W);

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            scanf("%d", &G[i][j]);
        }
    }

    printf("%d\n", getLargestSquare(H, W));

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 18264
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < (int)(n); i++)
using ll = long long;

int main()
{
    int h;
    int w;
    int i;
    int j;
    int c;
    int maxp;
    int minp;

    cin >> h >> w;
    vector<vector<int>> DP(h, vector<int>(w, 0));
    maxp = 0;
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> c;
            if (c == 0)
            {
                DP.at(i).at(j) = 1;
                maxp = 1;
            }
        }
    }
    for (i = 1; i < h; i++)
    {
        for (j = 1; j < w; j++)
        {
            if (DP.at(i).at(j) == 0)
            {
                continue;
            }
            DP.at(i).at(j) = min(DP.at(i - 1).at(j), min(DP.at(i).at(j - 1), DP.at(i - 1).at(j - 1))) + 1;
            maxp = max(maxp, DP.at(i).at(j));
        }
    }
    cout << maxp * maxp << endl;

    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 10684
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < (int)(n); i++)
using ll = long long;

int main()
{
    int h;
    int w;
    int i;
    int j;
    int c;
    int maxp;
    int minp;

    cin >> h >> w;
    vector<vector<int>> DP(h, vector<int>(w, 0));
    maxp = 0;
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> c;
            if (c == 0)
            {
                DP.at(i).at(j) = 1;
                maxp = 1;
            }
        }
    }
    for (i = 1; i < h; i++)
    {
        for (j = 1; j < w; j++)
        {
            if (DP.at(i).at(j) == 0)
            {
                continue;
            }
            minp = min(DP.at(i - 1).at(j), min(DP.at(i).at(j - 1), DP.at(i - 1).at(j - 1)));
            DP.at(i).at(j) = minp + 1;
            maxp = max(maxp, DP.at(i).at(j));
        }
    }
    cout << maxp * maxp << endl;

    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 10760
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < (int)(n); i++)
using ll = long long;

int main()
{
    int h;
    int w;
    int i;
    int j;
    int c;
    int maxp;
    int minp;

    cin >> h >> w;
    vector<vector<int>> DP(h, vector<int>(w, 0));
    maxp = 0;
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> c;
            if (c == 0)
            {
                DP.at(i).at(j) = 1;
                maxp = 1;
            }
        }
    }
    for (i = 1; i < h; i++)
    {
        for (j = 1; j < w; j++)
        {
            if (DP.at(i).at(j) == 0)
            {
                continue;
            }
            minp = min(DP.at(i - 1).at(j), DP.at(i).at(j - 1));
            if (DP.at(i - minp).at(j - minp) != 0)
            {
                DP.at(i).at(j) = minp + 1;
                maxp = max(maxp, DP.at(i).at(j));
            }
            else
            {
                DP.at(i).at(j) = minp;
            }
        }
    }
    cout << maxp * maxp << endl;

    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 10736
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (i = 0; i < (int)(n); i++)
using ll = long long;

int main()
{
    int h;
    int w;
    int i;
    int j;
    int c;
    int maxp;
    int minp;

    cin >> h >> w;
    vector<vector<int>> DP(h, vector<int>(w, 0));
    rep(i, h)
    {
        rep(j, w)
        {
            cin >> c;
            if (c == 0)
            {
                DP.at(i).at(j) = 1;
            }
        }
    }
    maxp = DP.at(0).at(0);
    for (i = 1; i < h; i++)
    {
        for (j = 1; j < w; j++)
        {
            if (DP.at(i).at(j) == 0)
            {
                continue;
            }
            minp = min(DP.at(i - 1).at(j), DP.at(i).at(j - 1));
            if (DP.at(i - minp).at(j - minp) != 0)
            {
                DP.at(i).at(j) = minp + 1;
                maxp = max(maxp, DP.at(i).at(j));
            }
            else
            {
                DP.at(i).at(j) = minp;
            }
        }
    }
    cout << maxp * maxp << endl;

    return 0;
}
accuracy: 0.775
time_ms: 30
memory_kb: 5020
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int MAX = 1400;

int M[MAX][MAX], dp[MAX][MAX];

int getLargestSquare(int H, int W) {
	int maxWidth = 0;
	
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (M[i][j]) dp[i][j] = 0;
			else if (i == 0 || j == 0) {
				dp[i][j] = 1;
				maxWidth = max(maxWidth, dp[i][j]);
			}
			else {
				dp[i][j] = min({dp[i-1][j-1], dp[i-1][j], dp[i][j-1]}) + 1;
				maxWidth = max(maxWidth, dp[i][j]);
			}
		}
	}
	
	return maxWidth * maxWidth;
}

int main() {
	int H, W; cin >> H >> W;
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			cin >> M[i][j];
		}
	}
	
	cout << getLargestSquare(H, W) << endl;
	
	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 18632
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int MAX = 1400;

int M[MAX][MAX], dp[MAX][MAX];

int getLargestSquare(int H, int W) {
	int maxWidth = 0;
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			dp[i][j] = M[i][j] ? 0 : 1;
			if (dp[i][j]) maxWidth = 1;
		}
	}
	
	for (int i = 1; i < H; ++i) {
		for (int j = 1; j < W; ++j) {
			if (!dp[i][j]) continue;
			dp[i][j] = min({dp[i-1][j-1], dp[i-1][j], dp[i][j-1]}) + 1;
			maxWidth = max(maxWidth, dp[i][j]);
		}
	}
	
	return maxWidth * maxWidth;
}

int main() {
	int H, W; cin >> H >> W;
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			cin >> M[i][j];
		}
	}
	
	cout << getLargestSquare(H, W) << endl;
	
	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 18652
final_rank: 11