[Java] ✅ PREFIX SUM ✅ CLEAN CODE ✅ CLEAN EXPLANATIONS
Approach\n1. Compute the row and column prefixes of count of 1 and store them in an 2 x int[][] \n    - EG: [[1,1,0,0,1],[0,1,0,1,0] \n    - rowPrefix [[1,2,2,2
# Approach\n1. Compute the row and column prefixes of count of 1 and store them in an 2 x int[][] \n    - EG: [[1,1,0,0,1],[0,1,0,1,0] \n    - rowPrefix [[1,2,2,2,3],[0,1,1,2,2]] -> count of 1s on each row\n    - colPrefix [[1,1,0,0,1],[1,2,0,1,1]] -> count of 1s on each column by row.\n2. Knowing this, we can apply interval count:\n    - number of 1s at on row 0 between index 3 and 7 is rowPrefix[0][7] - rowPrefix[0][2].\n    - number of 1s on column 3 between rows 2 and 8 is colPrefix[8][3] - colPrefix[1][3]\n3. In 2 fors (i - rows, j columns, stopping when i,j < length - range)\n    - Find the further diagonal point from i & j that sits at the edge of a square.\n    - the furthest point if Math.min(n - i, m - j)\n    - From the furthest, try to determine if the 4 borders (aka interval counts) equal range.\n    - Stop when you find a range or when current range goes below the largest range found so far.\n\n# Complexity\n- Time complexity:$$O(n * m * min(m, n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n * m)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public int largest1BorderedSquare(int[][] grid) {\n        int[][] rowPrefix = new int[grid.length][grid[0].length];\n        int[][] colPrefix = new int[grid.length][grid[0].length];\n        getPrefixes(grid, rowPrefix, colPrefix);\n        return findLargestBorder(grid, rowPrefix, colPrefix);\n    }\n\n    private void getPrefixes(int[][] grid, int[][] rowPrefix, int[][] colPrefix) {\n        for (int i = 0; i < grid.length; i++) {\n            for (int j = 0; j < grid[0].length; j++) {\n                rowPrefix[i][j] = grid[i][j] + (j == 0 ? 0 : rowPrefix[i][j - 1]);\n                colPrefix[i][j] = grid[i][j] + (i == 0 ? 0 : colPrefix[i - 1][j]);\n            }\n        }\n    }\n\n    private int findLargestBorder(int[][] grid, int[][] rowPrefix, int[][] colPrefix) {\n        int largest = 0;\n        boolean found = false;\n        for (int i = 0; i < grid.length - largest; i++) {\n            for (int j = 0; j < grid[i].length - largest; j++) {\n                if (grid[i][j] == 1) {\n                    found = true;\n                    largest = Math.max(largest, shrinkBorder(grid, i, j, rowPrefix, colPrefix, largest));\n                }\n            }\n        }\n        return !found ? 0 : (int)Math.pow(1 + largest, 2);\n    }\n\n    private int shrinkBorder(int[][] grid, int x, int y, int[][] rowPrefix, int[][] colPrefix, int largest) {\n        int range = Math.min(grid.length - x, grid[0].length - y) - 1;\n        while (range > largest) {\n            if (isValidBorder(grid, x, y, range, rowPrefix, colPrefix)) {\n                break;\n            }\n            range--;\n        }\n        return range; \n    }\n\n    private boolean isValidBorder(int[][] grid, int x, int y, int range, int[][] rowPrefix, int[][] colPrefix) {\n        return 1 + range == rowPrefix[x][y + range] - (y == 0 ? 0 : rowPrefix[x][y-1])\n            && 1 + range == rowPrefix[x + range][y + range] - (y == 0 ? 0 : rowPrefix[x + range][y-1])\n            && 1 + range == colPrefix[x + range][y] - (x == 0 ? 0 : colPrefix[x-1][y])\n            && 1 + range == colPrefix[x + range][y + range] - (x == 0 ? 0 : colPrefix[x-1][y + range]);\n    }\n}\n```
['Prefix Sum', 'Java']