#include<iostream>usingnamespacestd;inta[1001][1001],dp[1001][1001];intmain(){intn,m;cin>>n>>m;for(inti=1;i<=n;i++){for(intj=1;j<=m;j++){cin>>a[i][j];}}intans=0;for(inti=1;i<=n;i++){for(intj=1;j<=m;j++){if(a[i][j]==1){dp[i][j]=1+min(dp[i-1][j-1],min(dp[i-1][j],dp[i][j-1]));//left up leftup
}ans=max(ans,dp[i][j]);}}cout<<ans<<endl;return0;}