<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:
    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
        #dp[i][j] = (a, b, c, d) which is the length of contiguois ones in that direction
        for row in grid:
            print(row)
        print()
        m = len(grid)
        n = len(grid[0])
        dp = [[[0, 0, 0, 0] for j in range(n)] for i in range(m)]
        
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 0:
                    continue
                    
                dp[i][j][0] = 1 + (dp[i][j-1][0] if j-1 >= 0 else 0)
                dp[i][j][1] = 1 + (dp[i-1][j][1] if i-1 >= 0 else 0)             
                
        for i in range(m-1, -1, -1):
            for j in range(n-1, -1, -1):
                if grid[i][j] == 0:
                    continue
                    
                dp[i][j][2] = 1 + (dp[i][j+1][2] if j+1 < n else 0)
                dp[i][j][3] = 1 + (dp[i+1][j][3] if i+1 < m else 0)
        
        for row in dp:
            print(row)
        for s in range(min(m, n), 0, -1):
            for i in range(m-s+1):
                for j in range(n-s+1):
                    canDo = min(dp[i][j][2], dp[i][j][3]) >= s and min(dp[i+s-1][j+s-1][0], dp[i+s-1][j+s-1][1]) >= s
                    if canDo:
                        return s**2
        return 0
                
18000