[Java] Built with vertical and horizontal grids
\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        \n        int m = grid.length;\n        int n = grid[0].length;\n        \n  
```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        \n        int m = grid.length;\n        int n = grid[0].length;\n        \n        int[][] ver = new int[m][n];\n        int[][] hor = new int[m][n];\n        \n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < m; j++) {\n                if (grid[j][i] == 1) {\n                    ver[j][i] = j == 0 ? ver[j][i] = 1 : ver[j-1][i] + 1;   \n                } else {\n                    ver[j][i] = 0;\n                }\n            }\n        }\n        \n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (grid[i][j] == 1) {\n                    hor[i][j] = j == 0 ? hor[i][j] = 1 : hor[i][j-1] + 1;\n                } else {\n                    hor[i][j] = 0;\n                }\n            }\n        }\n        \n        int max = 0;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                \n                boolean found = false;\n                for (int len = Math.min(ver[i][j], hor[i][j]); len > max && !found; len--) {\n                    \n                    if (ver[i][j+1-len] >= len && hor[i+1-len][j] >= len) {\n                        max = len;\n                        found = true;\n                    }\n                }\n            }\n        }\n        \n        return max * max;\n    }\n}\n```
[]