C++ straightforward matrix
\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        vector<vecto
```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        int m = grid.size(), n = grid[0].size();\n        vector<vector<int>> mr(m, vector<int>(n, 0));\n        vector<vector<int>> mc(m, vector<int>(n, 0));\n        \n        for(int i=0; i<m; ++i){\n            for(int j=0; j<n; ++j){\n                if(j == 0) {\n                    mr[i][j] = grid[i][j];\n                } else {\n                    mr[i][j] = grid[i][j] == 0 ? 0 : mr[i][j-1] + 1;\n                }\n            }\n        }\n        \n        for(int j=0; j<n; ++j){\n            for(int i=0; i<m; ++i){\n                if(i == 0) {\n                    mc[i][j] = grid[i][j];\n                } else {\n                    mc[i][j] = grid[i][j] == 0 ? 0 : mc[i-1][j] + 1;\n                }\n            }\n        }\n        \n        int ans = 0;\n        for(int i=0; i<m; ++i) {\n            for(int j=0; j<n; ++j) {\n                for(int len=0; i+len < m && j+len < n; ++len) {\n                    if(mr[i][j+len] > len && mc[i+len][j] > len && mr[i+len][j+len] > len && mc[i+len][j+len] > len) {\n                        ans = max(ans, len+1);\n                    }\n                }\n            }\n        }\n        \n        return ans * ans;\n    }\n};\n```
[]