Python Union-Find Solution
\nclass Solution:\n    def makeSet(self, a):\n        # Initially, a new element is in its own set\n        # its parent is itself, its rank is 0\n        self.
```\nclass Solution:\n    def makeSet(self, a):\n        # Initially, a new element is in its own set\n        # its parent is itself, its rank is 0\n        self.parent[a] = a\n        self.rank[a] = 0\n        self.size[a] = 1\n\n    def find(self, a):\n        # find the root representative of the set that a belongs to\n        if (self.parent[a] != a):\n            self.parent[a] = self.find(self.parent[a])\n        return self.parent[a]\n\n    def union(self, a, b):\n        # union the set that includes a and  the set that includes b\n        a_root = self.find(a)\n        b_root = self.find(b)\n        if a_root != b_root:\n            if self.rank[a_root] < self.rank[b_root]:\n                a_root, b_root = b_root, a_root\n            self.parent[b_root] = a_root\n            self.size[a_root] += self.size[b_root]\n            if self.rank[a_root] == self.rank[b_root]:\n                self.rank[b_root] += 1\n            self.answer = max(self.answer, self.size[a_root])\n            \n    def largestIsland(self, grid: List[List[int]]) -> int:\n        self.answer = 0\n        self.parent, self.rank, self.size = dict(), dict(), dict()\n        n, m = len(grid), len(grid[0])\n        # make set for each cell of island, and find the largest size of island\n        for x in range(n):\n            for y in range(m):\n                if grid[x][y] == 1:\n                    self.makeSet((x, y)) # make set for a new island (x, y)\n                    self.answer = max(self.answer, 1)\n                    for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:\n                        x1, y1 = x + dx, y + dy\n                        if 0 <= x1 < n and 0 <= y1 < m and (x1, y1) in self.parent:\n                            # union (x, y) and its neighbor (x1, y1)\n                            self.union((x1, y1), (x, y))\n        # change each 0 to 1, and find the largest size of island\n        for x in range(n):\n            for y in range(m):\n                if grid[x][y] == 0:\n                    root_set = set()\n                    for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:\n                        x1, y1 = x + dx, y + dy\n                        if 0 <= x1 < n and 0 <= y1 < m and grid[x1][y1] == 1:\n                            root = self.find((x1, y1))\n                            root_set.add(root)\n                    self.answer = max(self.answer, 1 + sum([self.size[root] for root in root_set]))\n        return self.answer\n```
[]