Extremely Simple Concept Using Marker
Mark all the Connected Components in the grid using a distinct Integer marker. \n\nKeep the count of the marker in the grid using a HashMap. \n\nFor each zero c
Mark all the Connected Components in the grid using a distinct Integer marker. \n\nKeep the count of the marker in the grid using a HashMap. \n\nFor each zero check the distinct islands that get connected. \n\nex: \n\n101\n000\n011\n\nusing Marker\nGrid : \n203\n000\n044\n\nMap : \n2 : 1\n3 : 1\n4 : 2\n\nNow for each zero, check the distinct marker it connects. \nThe zero in the 2nd row last column connects marker 3 and 4. \n\nHence the counts becomes maximum = 2 (count of 4) + 1 (count of 3) + 1(The zero that is converted to 1). \n\nHope this helps. \n\n```\nclass Solution {\n    public int dfs(int[][]grid, int i, int j , int marker){\n        int count = 0 ;\n        if(i<0 || j <0 || i >= grid.length || j >= grid[0].length || grid[i][j] != 1)return count; \n        grid[i][j] = marker; \n        count++; \n        count += dfs(grid, i-1, j, marker) + dfs(grid, i+1, j, marker) +dfs(grid, i, j-1, marker) +dfs(grid, i, j+1, marker); \n        return count; \n    }\n    \n    public int calc(int[][] grid, int i, int j, Map<Integer, Integer> map){\n        int count = 0; \n        Set<Integer> set = new HashSet<>(); \n        for(int x = Math.max(0, i-1); x <= Math.min(grid.length-1, i+1); ++x){\n            for(int y = Math.max(0, j-1); y<=Math.min(grid[0].length-1, j+1); ++y){\n                if(x == i && y == j)continue; \n\t\t\t\t\t\t\t\t// Continue if its the diagonal elements\n                if((x==i-1 && y==j-1) || (x==i+1 && y==j+1) || (x==i+1 && y==j-1) || (x==i-1 && y==j+1))continue; \n                if(grid[x][y]!=0 && !set.contains(grid[x][y]))count+=map.get(grid[x][y]);\n                set.add(grid[x][y]); \n            }\n        }\n        return count+1; \n    }\n    \n    public int largestIsland(int[][] grid) {\n        int marker = 2;\n        if(grid == null || grid.length < 1)return 0; \n        int max = 0; \n        Map<Integer, Integer> map = new HashMap<>(); \n        for(int i = 0 ; i < grid.length ; ++i){\n            for(int j = 0 ; j < grid[0].length ; ++j){\n                if(grid[i][j] == 1){\n                    int count = dfs(grid, i, j, marker++); \n                    map.put(marker-1, count);\n                    max = Math.max(max, count); \n                }\n            }\n        }\n        for(int i = 0 ; i < grid.length; ++i){\n            for(int j = 0 ; j < grid[0].length ; ++j){\n                if(grid[i][j] == 0){\n                    max = Math.max(max, calc(grid, i, j, map ));\n                }\n            }\n        }\n        return max; \n    }\n}\n```
[]