Java Solution using Union Find
\nclass Solution {\n    public int largestIsland(int[][] grid) {\n        int rows = grid.length;\n        int cols = grid[0].length;\n        \n        // crea
```\nclass Solution {\n    public int largestIsland(int[][] grid) {\n        int rows = grid.length;\n        int cols = grid[0].length;\n        \n        // create father array and size array, and initialize them\n        int[] father = new int[rows * cols];\n        for (int i = 0; i < rows * cols; i++) {\n            father[i] = i;\n        }\n        int[] size = new int[rows * cols];\n        Arrays.fill(size, 1);\n        \n        int[] dx = {0, 1, -1, 0};\n        int[] dy = {1, 0, 0, -1};\n        \n        // scan grid, update father array and size array\n        for (int i = 0; i < rows; i++) {\n            for (int j = 0; j < cols; j++) {\n                if (grid[i][j] == 1) {\n                    int id = i * cols + j;\n                    for (int k = 0; k < 4; k++) {\n                        int newi = i + dx[k];\n                        int newj = j + dy[k];\n                        int newid = newi * cols + newj;\n                        if (isValid(rows, cols, newi, newj) && grid[newi][newj] == 1) {\n                            union(father, size, id, newid);\n                        }\n                    }\n                }\n            }\n        }\n        \n        // find current max component size\n        int max = 0;\n        for (int i = 0; i < size.length; i++) {\n            max = Math.max(max, size[i]);\n        }\n        \n        // find max component size if we set any 0 to 1\n        for (int i = 0; i < rows; i++) {\n            for (int j = 0; j < cols; j++) {\n                if (grid[i][j] == 0) {\n                    int id = i * cols + j;\n                    int combinedSize = 1;\n                    Set<Integer> prevFather = new HashSet<>();\n                    for (int k = 0; k < 4; k++) {\n                        int newi = i + dx[k];\n                        int newj = j + dy[k];\n                        int newid = newi * cols + newj;\n                        if (isValid(rows, cols, newi, newj) && grid[newi][newj] == 1) { \n                            int currFather = find(father, newid);\n                            if (prevFather.isEmpty() || !prevFather.contains(currFather)) {\n                                combinedSize += size[currFather];\n                                prevFather.add(currFather);\n                            }\n                        }\n                    }\n                    max = Math.max(max, combinedSize);\n                }\n            }\n        }\n        \n        // return whole size if the grid is an all 1 matrix, otherwise return the value of max\n        return max == 0 ? rows * cols : max;\n    }\n    \n    public int find(int[] father, int id) {\n        if (father[id] == id) {\n            return id;\n        }\n        return father[id] = find(father, father[id]);\n    }\n    \n    public void union(int[] father, int[] size, int id1, int id2) {\n        int fa1 = find(father, id1);\n        int fa2 = find(father, id2);\n        if (fa1 != fa2) {\n            father[fa1] = fa2;\n            size[fa2] += size[fa1];\n        }\n    }\n    \n    public boolean isValid(int rows, int cols, int i, int j) {\n        if (i >= 0 && i < rows && j >= 0 && j < cols) {\n            return true;\n        }\n        return false;\n    }\n}\n```\n
[]