Java Solution || 84% fast || 63% memory || DFS || Easy to understand
Below is my commented solution. Please share any comments below.\n\n\nclass Solution {\n    /*\n        Approach:\n            \n        Two iterations through 
Below is my commented solution. Please share any comments below.\n\n```\nclass Solution {\n    /*\n        Approach:\n            \n        Two iterations through the grid:\n            In first iteration through the grid, use DFS to count and group separate islands together.\n                * Using a map to map groupNumber -> sizeOfIsland\n            In second iteration, look for any \'0\' and add the values of all the unique groups from 4 directions.\n            \n        Complexities:\n            * Time: O(n^2) - We are iterating through the grid twice. Even in DFS, we are not DFS-ing on visited indices.\n            * Space: O(n^2) - We are creating group array to account for visited and for groupings\n    */\n    \n    private int[][] dirs = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };\n    \n    public int largestIsland(int[][] grid) {\n        int len = grid.length;\n        int largest = 0;\n    \n        int[][] group = new int[len][len];\n        Map<Integer, Integer> groupMap = new HashMap<>();\n        int groupNum = 1;\n        \n        \n        for (int i = 0; i < len; i++) {\n            for (int n = 0; n < len; n++) {\n                // If group[i][n] > 0, it means it was already calculated and assigned a group, we can skip\n                if (grid[i][n] == 1 && group[i][n] == 0) {\n                    group[i][n] = groupNum;\n                    int sizeOfIsland = dfs(grid, i, n, group, groupNum); // the size of this group\n                    groupMap.put(groupNum, sizeOfIsland);\n                    groupNum++;\n                    \n                    largest = Integer.max(largest, sizeOfIsland);\n                }\n            }\n        }\n        \n        for (int i = 0; i < len; i++) {\n            for (int n = 0; n < len; n++) {\n                if (grid[i][n] == 0) {\n                    int count = 0;\n                    \n                    Set<Integer> dup = new HashSet<>();\n                    for (int[] dir : dirs) {\n                        int x = i + dir[0];\n                        int y = n + dir[1];\n                        if (x >= 0 && y >= 0 && x < len && y < len && group[x][y] > 0 && !dup.contains(group[x][y])) {\n                            count+= groupMap.get(group[x][y]);\n                            dup.add(group[x][y]);\n                        }\n                    }\n                    \n                    largest = Integer.max(largest, count+1); // we add 1 to include the current changed \'0\'\n                }\n            }\n        }\n        \n        return largest;\n    }\n    \n    private int dfs(int[][] grid, int i, int n, int[][] group, int groupNum) {\n        int count = 0;\n        \n        for (int[] dir : dirs) {\n            int x = i + dir[0];\n            int y = n + dir[1];\n            if (x >= 0 && y >= 0 && x < grid.length && y < grid.length && grid[x][y] == 1 && group[x][y] == 0) {\n                group[x][y] = groupNum;\n                count += dfs(grid, x, y, group, groupNum);\n            }\n        }\n        \n        return count + 1;\n    }\n    \n}\n```
[]