C++ O(n²)time and O(n²)space
use left and up to record the numbers of one along two lines\u2014\u2014row and column\nthen use the requirement that the area should be square to select suitab
use left and up to record the numbers of one along two lines\u2014\u2014row and column\nthen use the requirement that the area should be square to select suitable situation\nFinally,it should be paid attention that we should try every situation until the number is equal to current max number\n\n```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m=grid.size(),n=grid[0].size();\n        int res=0;\n        vector<vector<int>> left(m,vector<int>(n,0)),up(m,vector<int>(n,0));\n        for(int i=0;i<m;++i){\n            for(int j=0;j<n;++j){\n                left[i][j]= grid[i][j]==0?0:j>=1?left[i][j-1]+1:grid[i][0];\n                up[i][j]= grid[i][j]==0?0:i>=1?up[i-1][j]+1:grid[0][j];\n                int t=min(left[i][j],up[i][j]);\n                while(t>res){\n                    int U=i-t+1,L=j-t+1;\n                    if(up[i][L]>=t && left[U][j]>=t) res=max(res,t);\n                    --t;\n                }\n            }\n        }\n        return res*res;\n    }\n};\n```
[]