Kotlin Best Solution
\n# Code\n\nclass Solution {\nfun largestIsland(grid: Array<IntArray>): Int {\n        if (grid.isEmpty()) return 0\n        val R = grid.size\n        val C = 
\n# Code\n```\nclass Solution {\nfun largestIsland(grid: Array<IntArray>): Int {\n        if (grid.isEmpty()) return 0\n        val R = grid.size\n        val C = grid[0].size\n        val dsu = DSU(R * C)\n        val dirs = listOf(0 to 1, 0 to -1, 1 to 0, -1 to 0)\n        var maxArea = 0\n        \n        fun encode(r: Int, c: Int) = r * C + c\n        \n        fun isLegal(x: Int, y: Int) = x in 0 until R && y in 0 until C\n        \n        for (i in 0 until R) {\n            for (j in 0 until C) {\n                if (grid[i][j] == 1) {\n                    val currentCell = encode(i, j)\n                    for ((deltaI, deltaJ) in dirs) {\n                        val (newX, newY) = i + deltaI to j + deltaJ\n                        if (isLegal(newX, newY).not()) continue\n                        if (grid[newX][newY] != 1) continue\n                        val newCell = encode(newX, newY)\n                        dsu.union(currentCell, newCell)\n                    }\n                }\n            }\n        }\n        \n        maxArea = dsu.maxSize\n        \n        for (i in 0 until R) {\n            for (j in 0 until C) {\n                if (grid[i][j] == 0) {\n                    val seen: MutableSet<Int> = HashSet()\n                    val currentCell = encode(i, j)\n                    var currentAns = 1\n                    for ((deltaI, deltaJ) in dirs) {\n                        val (newX, newY) = i + deltaI to j + deltaJ\n                        if (isLegal(newX, newY).not()) continue\n                        if (grid[newX][newY] != 1) continue\n                        val newCell = encode(newX, newY)\n                        val newCellParent = dsu.find(newCell)\n                        if (seen.contains(newCellParent).not()) {\n                            currentAns += dsu.size[newCellParent]\n                            seen.add(newCellParent)\n                        }\n                    }\n                    maxArea = maxOf(maxArea, currentAns)\n                }\n            }\n        }\n        return maxArea\n    }\n }\nclass DSU(val N: Int) {\n    val parent = IntArray(N)\n    val size = IntArray(N)\n    var maxSize = 1\n    \n    init {\n        makeSet()\n    }\n    \n    private fun makeSet() {\n        for (i in 0 until N) {\n            parent[i] = i\n            size[i] = 1\n        }\n    }\n    \n    fun find(x: Int): Int {\n        if (parent[x] != x) {\n            parent[x] = find(parent[x])\n        }\n        return parent[x]\n    }\n    \n    fun union(x: Int, y: Int): Boolean {\n        val (xP, yP) = find(x) to find(y)\n        if (xP == yP) return false\n        if (size[xP] > size[yP]) {\n            size[xP] += size[yP]\n            parent[yP] = xP\n            maxSize = maxOf(maxSize, size[xP])\n        } else {\n            size[yP] += size[xP]\n            parent[xP] = yP\n            maxSize = maxOf(maxSize, size[yP])\n        }\n        return true\n    }\n}\n```
['Kotlin']