C#, O(mn * min(m,n)) DP solution, beats 100%
\npublic class Solution {\n    public int Largest1BorderedSquare(int[][] grid) {\n        int height = grid.Length;\n        int width = grid[0].Length;\n      
```\npublic class Solution {\n    public int Largest1BorderedSquare(int[][] grid) {\n        int height = grid.Length;\n        int width = grid[0].Length;\n        int border = 0;\n        var dp = new (int Left, int Top)[height+1][];\n        for (int row = 0; row <= height; row++)\n            dp[row] = new (int Left, int Top)[width+1];\n        for (int i = 1; i <= height; i++) {\n            for (int j = 1; j <= width; j++) {\n                if (grid[i-1][j-1] == 0)\n                    continue;\n                border = Math.Max(border, 1);\n                int left = dp[i][j-1].Left;\n                int top = dp[i-1][j].Top;\n                int corner = Math.Min(left, top);\n                while (corner > 0) {\n                    if (Math.Min(dp[i-corner][j].Left, dp[i][j-corner].Top) >= corner+1) {\n                        border = Math.Max(border, corner+1);\n                        break;\n                    }\n                    corner--;\n                }\n                dp[i][j].Left = left + 1;\n                dp[i][j].Top = top + 1;\n            }\n        }\n        \n        return border*border;\n    }\n}\n```
['Dynamic Programming', 'C#']