Java DP Solution, beats 100%(both time and memory)
\u8BB0\u5F55\u6BCF\u4E2A\u70B9\uFF08\u7B97\u8FD9\u4E2A\u70B9\u672C\u8EAB\uFF09\u5DE6\u8FB9\u8FDE\u7EED\u76841\u7684\u4E2A\u6570\uFF0C\u4E0A\u8FB9\u8FDE\u7EED\u7
\u8BB0\u5F55\u6BCF\u4E2A\u70B9\uFF08\u7B97\u8FD9\u4E2A\u70B9\u672C\u8EAB\uFF09\u5DE6\u8FB9\u8FDE\u7EED\u76841\u7684\u4E2A\u6570\uFF0C\u4E0A\u8FB9\u8FDE\u7EED\u76841\u7684\u4E2A\u6570\u3002\u7136\u540E\u9009\u4E24\u8005\u4E2D\u8F83\u5C0F\u7684\u90A3\u4E2Ax\uFF0C\u5206\u522B\u4ECEx\u52301\u4F5C\u4E3A\u8FB9\u957F\uFF0C\u770B\u662F\u5426\u5B58\u5728\u5408\u6CD5\u7684\u6B63\u65B9\u5F62\u3002\nmemorize  the number of consecutive 1s on the top and on the left for every cell (include the cell itself)\uFF0Cpick the minimum one as side length of potential quare. (sorry for my awful English)\n```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int m = grid.length, n = grid[0].length;\n        int[][] left = new int[m][n], top = new int[m][n];\n        int res = 0;\n        for (int i = 0; i < m; i++) {\n            for (int j = 0; j < n; j++) {\n                if (i == 0) {\n                    top[i][j] = grid[i][j];\n                } else {\n                    top[i][j] = grid[i][j] == 0? 0 : top[i-1][j] + 1;\n                }\n                if (j == 0) {\n                    left[i][j] = grid[i][j];\n                } else {\n                    left[i][j] = grid[i][j] == 0? 0 : left[i][j-1] + 1;\n                }\n                for (int l = Math.min(top[i][j], left[i][j]); l >= 1; l--) {\n\t\t\t\t// \u5DF2\u77E5\u4E0B\u9762\u7684\u8FB9\u548C\u53F3\u4FA7\u8FB9\u5408\u6CD5\uFF0C\u770B\u4E0A\u8FB9\u548C\u5DE6\u4FA7\u8FB9\u662F\u5426\u5408\u6CD5\n                    if (left[i-l+1][j] >= l && top[i][j-l+1] >= l) {\n                        res = Math.max(res, l * l);\n                        break;\n                    }\n                }\n            }\n        }\n        return res;\n    }\n}\n```\n
['Dynamic Programming', 'Java']