[C++] Compact solution, 12 lines
\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        vector<vector<int>> U(grid), L(grid);  // up, left\n        si
```\nclass Solution {\npublic:\n    int largest1BorderedSquare(vector<vector<int>>& grid) {\n        vector<vector<int>> U(grid), L(grid);  // up, left\n        size_t largest = 0;\n        for (size_t i = 0; i < grid.size(); i++)\n            for (size_t j = 0; j < grid[0].size(); j++)\n                if (grid[i][j]) {\n                    L[i][j] += j > 0 ? L[i][j-1] : 0;\n                    U[i][j] += i > 0 ? U[i-1][j] : 0;\n                    for (size_t lim = min(L[i][j], U[i][j]), k = largest; k < lim; k++)\n                        if (L[i-k][j] > k && U[i][j-k] > k)\n                            largest = k+1;\n                }\n        return largest*largest;\n    }\n};\n```
['Dynamic Programming', 'C++']