[Java] One pass 12 liner DP code w/ explanation and analysis.
Use two 2-D arrays to record the count of consecutive 1\'s horizontally and vertically, respectively;\n2. a) horizontal[i + 1][j + 1] and horizontal[i + 2 - len
1. Use two 2-D arrays to record the count of consecutive `1\'s` horizontally and vertically, respectively;\n2. a) `horizontal[i + 1][j + 1]` and `horizontal[i + 2 - len][j + 1]` indicate the count of consecutive `1\'s` ending at `j` on row `i` and `i + 1 - len`, respectively; \nb) `vertical[i + 1][j + 1]` and `vertical[i + 1][j + 2 - len]` indicate the count of consecutive `1\'s` ending at `i` on column `j` and `j + 1 - len`, respectively; \n3. The minimum of the above 4 values in the arrays is greater or equal to `len`, then `len` is the length of the required square.\n4. Loop through `grid` to update the max area of the square met the requirement. \n\n```\n    public int largest1BorderedSquare(int[][] grid) {\n        int area = 0, m = grid.length, n = m == 0 ? 0 : grid[0].length;\n        int[][] horizontal = new int[m + 1][n + 1], vertical = new int[m + 1][n + 1];\n        for (int i = 0; i < m; ++i) {\n            for (int j = 0; j < n; ++j) {\n                horizontal[i + 1][j + 1] = grid[i][j] == 0 ? 0 : 1 + horizontal[i + 1][j]; \n                vertical[i + 1][j + 1] = grid[i][j] == 0 ? 0 : 1 + vertical[i][j + 1]; \n                for (int len = Math.min(horizontal[i + 1][j + 1], vertical[i + 1][j + 1]); area < len * len; --len)\n                    if (Math.min(horizontal[i + 2 - len][j + 1], vertical[i + 1][j + 2 - len]) >= len)\n                        area = len * len;\n            }\n        }\n        return area;\n    }\n```\n\n**Analysis:**\n\n Time: O(m * n * min(m, n)), space: O(m * n), where m = grid.length, n = grid[0].length.
[]