Easy-understanding solution
\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m = grid.size();\n        int n= grid[0].size();\n        
```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m = grid.size();\n        int n= grid[0].size();\n        vector<vector<int>>left(m,vector<int>(n,0));\n        vector<vector<int>>down(m,vector<int>(n,0));\n        for(int i = 0;i<m;i++){\n            left[i][n-1] = grid[i][n-1];\n        }\n        for(int i = 0;i<m;i++){\n            for(int j = n-2;j>=0;j--){\n                //now make the left grid\n                if(grid[i][j] == 0){\n                    left[i][j] = 0;\n                }\n                else{\n                    left[i][j] += left[i][j+1]+1;\n                }\n            }\n        }\n        for(int i = 0;i<n;i++){\n            down[m-1][i] = grid[m-1][i];\n        }\n        for(int i = m-2;i>=0 ;i--){\n            for(int j= 0;j<n;j++){\n                if(grid[i][j] == 0){\n                    down[i][j] = 0;\n                }\n                else{\n                    down[i][j]+= down[i+1][j]+1;\n                }\n            }\n        }\n        for(int i = 0;i<m;i++){\n            for(int j = 0;j<n;j++){\n                cout<<left[i][j] <<\' \';\n            }\n            cout<<endl;\n        }\n        cout<<endl;\n        for(int i = 0;i<m;i++){\n            for(int j = 0;j<n;j++){\n                cout<<down[i][j] <<\' \';\n            }\n            cout<<endl;\n        }\n        // now take the minimum of the values of the required\n        int ans = 0;\n        for(int i = 0;i<m;i++){\n            for(int j = 0;j<n;j++){\n                // take the minimum of the values of the \n                if(grid[i][j] == 1){\n                    int ci = i;\n                    int cj = j;\n                    int w = min(left[i][j],down[i][j]);\n                    // now serach in the left and down when no of the 1\'s become equal\n                    for(int k = 0;k<w;k++){\n                        // size of the square of the submatrix\n                        if(k == 0){\n                            ans = max(ans,1);\n                        }\n                        else{\n                            // now make it maximum value of the \n                            if(down[ci][cj+k] >= k+1 && left[ci+k][cj] >=k+1){\n                                ans = max(ans,(k+1)*(k+1));\n                            }\n                        }\n\n                    }\n                }\n                \n            }\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C']