Simple 9 lines iterative DP code with O(N*M) time


#1
int Solution::solve(vector<vector<int> > &A) {
int n=A.size(),m=A[0].size(),ans=0;;
for(int i=0;i<n;i++)
    for(int j=0;j<m;j++){
        if(A[i][j]&&i&&j) A[i][j]=min({A[i-1][j],A[i][j-1],A[i-1][j-1]})+1;
        ans=max(ans,A[i][j]);
    }
return ans*ans;

}