Javascript and C++ solutions
Synopsis:\n\nUse a naive DP algorithm to pre-calculate the prefix sequential sums per row and column of the input matrix A.  Then use these pre-calculations for
**Synopsis:**\n\nUse a naive DP algorithm to pre-calculate the prefix *sequential* sums per row and column of the input matrix `A`.  Then use these pre-calculations for O(1) lookups to determine if a sequential line of 1s exists for each of the 4 edges which can form a bordered sqaure.  Iterate through each cell of `i`,`j` of `A` and consider `i`,`j` as the top-left corner of a candidate bordered square of length `k`.  The corners of each candidate square are as follows for a square of length `k`:\n\n![image](https://assets.leetcode.com/users/claytonjwong/image_1591879021.png)\n\nThus, when checking for a square border, we need to check if the top/bottom rows of the border are all 1s and we need to check if the left/right columns of the border are all 1s.  The pre-calculated prefix sequential sums can be used to perform these 4 checks along the candidate square border by comparing the absolute difference between each corner cell\'s pre-calculated values and `k`.  If the difference is equal to `k`, then we know sequential 1s exist for the edge under consideration.\n\n**Example 1:**\n\n**Input:** A = [[1,1,1],[1,0,1],[1,1,1]]\n**Output:** 9\n\n![image](https://assets.leetcode.com/users/claytonjwong/image_1591880398.png)\n\nIn the diagram above, the answer is highlighted in green, (ie. a 3x3 square) starting at top-left cell `i = 0`, `j = 0`.  `k` is 0-base indexed, thus the border length `k = 2`, when we check each of the 4 sides:\n\n* Check the top/bottom rows highlighted in blue and observe `3 - 1 == 2` \u2705 \n\t* `row[i][j + k] - row[i][j] == k`\n\t* `row[i + k][j + k] - row[i + k][j] == k`\n* Check the left/right columns highlighted in yellow and observe `3 - 1 == 2` \u2705\n\t* `col[i + k][j] - col[i][j] == k`\n\t* `col[i + k][j + k] - col[i][j + k] == k`\n\nThis candidate square is `ok` since all 4 edges contain sequential 1s and the maximum side length `max` is then `k + 1` for edge end-points inclusive.\n\n---\n\n*Javascript*\n```\nlet largest1BorderedSquare = (A, max = 0) => {\n    let M = A.length,\n        N = A[0].length;\n    let row = [...Array(M)].map(_ => Array(N).fill(0)),\n        col = [...Array(M)].map(_ => Array(N).fill(0));\n    let ok = (i, j, k) => A[i][j]\n        && row[i][j + k] - row[i][j] == k           // top i-th row\n        && col[i + k][j] - col[i][j] == k           // left j-th col\n        && row[i + k][j + k] - row[i + k][j] == k   // bottom (i + k)-th row\n        && col[i + k][j + k] - col[i][j + k] == k;  // right (j + k)-th col\n    row[0][0] = A[0][0];\n    col[0][0] = A[0][0];\n    for (let i = 0; i < M; ++i)\n        for (let j = 0; j < N; ++j)\n            row[i][j] = !A[i][j] ? 0 : 1 + (0 <= j - 1 ? row[i][j - 1] : 0), // prefix sequential sums per row\n            col[i][j] = !A[i][j] ? 0 : 1 + (0 <= i - 1 ? col[i - 1][j] : 0); // prefix sequential sums per col\n    for (let i = 0; i < M; ++i)\n        for (let j = 0; j < N; ++j)\n            for (let k = 0; i + k < M && j + k < N; ++k)\n                max = Math.max(max, ok(i, j, k) ? k + 1 : 0); // k + 1 for edge end-points inclusive\n    return max * max; // max length squared == max 2D square area \uD83C\uDFAF\n};\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using VVI = vector<VI>;\n    int largest1BorderedSquare(VVI& A, int max = 0) {\n        int M = A.size(),\n            N = A[0].size();\n        VVI row(M, VI(N)),\n            col(M, VI(N));\n        auto ok = [&](auto i, auto j, auto k) {\n            return A[i][j]\n                && row[i][j + k] - row[i][j] == k           // top i-th row\n                && col[i + k][j] - col[i][j] == k           // left j-th col\n                && row[i + k][j + k] - row[i + k][j] == k   // bottom (i + k)-th row\n                && col[i + k][j + k] - col[i][j + k] == k;  // right (j + k)-th col\n        };\n        row[0][0] = A[0][0];\n        col[0][0] = A[0][0];\n        for (auto i{ 0 }; i < M; ++i)\n            for (auto j{ 0 }; j < N; ++j)\n                row[i][j] = !A[i][j] ? 0 : 1 + (0 <= j - 1 ? row[i][j - 1] : 0),\n                col[i][j] = !A[i][j] ? 0 : 1 + (0 <= i - 1 ? col[i - 1][j] : 0);\n        for (auto i{ 0 }; i < M; ++i)\n            for (auto j{ 0 }; j < N; ++j)\n                for (auto k{ 0 }; i + k < M && j + k < N; ++k)\n                    if (ok(i, j, k))\n                        max = std::max(max, k + 1); // k + 1 for edge end-points inclusive \n        return max * max; // max length squared == max 2D square area \uD83C\uDFAF\n    }\n};\n```
[]