Python3 Union Find
python\nclass Solution:\n    def largestIsland(self, grid: List[List[int]]) -> int:\n        N = len(grid)\n        \n        self.tree = {}\n        self.area 
```python\nclass Solution:\n    def largestIsland(self, grid: List[List[int]]) -> int:\n        N = len(grid)\n        \n        self.tree = {}\n        self.area = collections.defaultdict(int)\n        zero_cells = set()\n        \n        for i in range(N):\n            for j in range(N):\n                if grid[i][j] == 1:\n                    self.setRoot(i, j)\n                    for dx, dy in ((-1, 0), (0, -1), (1, 0), (0, 1)):\n                        nx, ny = i + dx, j + dy\n                        if 0 <= nx < N and 0 <= ny < N and (nx, ny) in self.tree: \n                            self.union((i, j), (nx, ny))                                             \n                else:\n                    zero_cells.add((i,j))\n                    \n        for key in self.tree:\n            root = self.findRoot(key)\n            self.area[root] += 1\n        \n        res = max(self.area.values()) if self.area.values() else 0\n        for i, j in zero_cells:\n            nei = set()\n            for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):\n                nx, ny = i + dx, j + dy\n                if 0 <= nx < N and 0 <= ny < N and grid[nx][ny] == 1:   \n                    nei.add(self.findRoot((nx, ny)))\n            res = max(res, 1 + sum(self.area[x] for x in nei))\n        return res       \n    \n    def union(self, a, b):\n        roota = self.findRoot(a)\n        rootb = self.findRoot(b)\n        \n        if roota != rootb:\n            self.tree[roota] = rootb\n        \n    def setRoot(self, x, y):\n        if (x, y) not in self.tree:\n            self.tree[(x,y)] = (x,y)\n            \n    def findRoot(self, a):\n        if a != self.tree[a]:\n            self.tree[a] = self.findRoot(self.tree[a])\n        return self.tree[a]\n```
[]