human
I found an interesting problem on https://leetcode.com/problems/largest-1-bordered-square/:
Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.
 
Example 1:
Input: grid = [[1,1,1],[1,0,1],[1,1,1]]
Output: 9

Example 2:
Input: grid = [[1,1,0,0]]
Output: 1

 
Constraints:

1 <= grid.length <= 100
1 <= grid[0].length <= 100
grid[i][j] is 0 or 1

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is the Python code you requested.

class Solution:
    
    \"\"\"
    1,1,1
    1,1,1
    1,1,1
    
    \"\"\"
                
                
    
    def largest1BorderedSquare(self, grid: List[List[int]]) -> int:
        
        
        def checkSquare(i, j, k):
            
            for ind in range(k+1):
                if grid[i][j+ind] == 0 or grid[i+ind][j] == 0 or grid[i+ind][k+j] == 0 or grid[i+k][j+ind] ==0:
                    return False
                
            return True
            
        n = len(grid)
        m = len(grid[0])
        max_ = 0

        for i in range(n):
            for j in range(m):
                
                if grid[i][j] == 1:
                    max_ = max(max_, 1)
                    
                    bound = min(n-i, m-j)
                    
                    
                    for k in range(1,bound):
                        if checkSquare(i,j, k):
                            max_ = max(max_, (k+1)**2)
                              
        return max_