<p>Given a 2D <code>grid</code> of <code>0</code>s and <code>1</code>s, return the number of elements in&nbsp;the largest <strong>square</strong>&nbsp;subgrid that has all <code>1</code>s on its <strong>border</strong>, or <code>0</code> if such a subgrid&nbsp;doesn&#39;t exist in the <code>grid</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
<strong>Output:</strong> 9
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,0,0]]
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= grid.length &lt;= 100</code></li>
	<li><code>1 &lt;= grid[0].length &lt;= 100</code></li>
	<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code></li>
</ul>
class Solution {
public:
    int largest1BorderedSquare(vector<vector<int>>& grid) {
        int side = 0;  // Variable to store the maximum side length of a 1-bordered square
        vector<vector<int>> row = grid, col = grid;  // Create two auxiliary grids to store row-wise and column-wise prefix sums of 1s

        // Compute the row-wise and column-wise prefix sums
        for(int i = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid[0].size(); j++) {
                // If the current cell has a 1, compute the prefix sums
                if(grid[i][j] == 1) { 
                    // row[i][j] stores the number of consecutive 1s to the left of (i, j) including itself
                    row[i][j] = (j > 0) ? 1 + row[i][j-1] : 1; 
                    // col[i][j] stores the number of consecutive 1s above (i, j) including itself
                    col[i][j] = (i > 0) ? 1 + col[i-1][j] : 1; 
                }
                // If the current cell is 0, set both row and col values to 0
                else { 
                    row[i][j] = 0;
                    col[i][j] = 0;
                }
            }
        }

        // Iterate through each cell in the grid to find the largest possible 1-bordered square
        for(int i = 0; i < grid.size(); i++) {
            for(int j = 0; j < grid[0].size(); j++) {
                int limit = min(row[i][j], col[i][j]);  // Limit is the smaller of row-wise or column-wise prefix sums at (i, j)

                // Check all possible square sizes that can end at (i, j)
                for(int mini = 1; mini <= limit; mini++) {
                    // Ensure that the square of size 'mini' is fully within bounds and has 1s on all borders
                    if(i - (mini - 1) >= 0 && j - (mini - 1) >= 0 && 
                       row[i - (mini - 1)][j] >= mini &&  // Check if top border has enough 1s
                       col[i][j - (mini - 1)] >= mini) {  // Check if left border has enough 1s
                        side = max(side, mini);  // Update the maximum possible side length
                    }
                }
            }
        }

        return side * side;  // Return the area of the largest 1-bordered square
    }
};

14300