Python solution
python\nclass Solution(object):\n    def largest1BorderedSquare(self, grid):\n        """\n        :type grid: List[List[int]]\n        :rtype: int\n        """
```python\nclass Solution(object):\n    def largest1BorderedSquare(self, grid):\n        """\n        :type grid: List[List[int]]\n        :rtype: int\n        """\n        if not grid:\n            return 0\n        \n        h = len(grid)\n        w = len(grid[0])\n        \n        ver = [[0 for i in range(w)] for i in range(h)]\n        hon = [[0 for i in range(w)] for i in range(h)]\n        \n        for i in range(w):\n            ver[0][i] = grid[0][i]\n        \n        for i in range(1, h):\n            for j in range(w):\n                if grid[i][j] == 1:\n                    ver[i][j] = 1 + ver[i - 1][j]\n        \n        \n        for i in range(h):\n            hon[i][0] = grid[i][0]\n            \n        for i in range(h):\n            for j in range(1, w):\n                if grid[i][j] == 1:\n                    hon[i][j] = 1 + hon[i][j - 1]\n        \n\n        res = 0\n        for i in range(h):\n            for j in range(w):\n                hv = hon[i][j]\n                vv = ver[i][j]\n                m = min(hv, vv)\n                k = 0\n                imax = 0\n                while m - k > 0:\n                    hk = hon[i - k][j]\n                    vk = ver[i][j - k]\n                    if hk > k and vk > k:\n                        imax = max(imax, (k + 1) ** 2)\n                    k += 1\n                    \n                res = max(res, imax)\n        \n                        \n        return res\n                    \n        \n```
[]