Readable Javascript Solution beats 100%
Strategy is to\n1. Paint islands with an unique integer > 1 (i.e. 2 then 3... so on)\n2. Loop through 0s and visit its neighboring islands.\n3. Return the max o
Strategy is to\n1. Paint islands with an unique integer > 1 (i.e. 2 then 3... so on)\n2. Loop through 0s and visit its neighboring islands.\n3. Return the max of neighboring islands + 1\n\nThis seems like a lot of code but actually it\'s really straight forward.\n\n```\nconst DIRECTIONS = [[1,0], [-1,0], [0,1], [0,-1]];\n\nfunction largestIsland(grid) {\n    if (!grid || !grid.length) return 0;\n\n    const m = grid.length;\n    const n = grid[0].length;\n    let max = Number.MIN_VALUE;\n    let count = 2;\n    let map = {};\n\n    for (let i = 0; i < m; i++) {\n        for (let j = 0; j < n; j++) {\n            if (grid[i][j] === 1) {\n                // mark the island\n                const area = dfs(grid, i, j, count);\n                max = Math.max(max, area);\n                map[count] = area;\n                count++;\n            }\n        }\n    }\n\n    for (let i = 0; i < m; i++) {\n        for (let j = 0; j < n; j++) {\n            if (grid[i][j] === 0) {\n                let set = new Set();\n                let cur = 1;\n\n                // visit it\'s neighbors\n                for (let [row, col] of DIRECTIONS) {\n                    const x = row + i;\n                    const y = col + j;\n\n                    // bound check\n                    if (x < 0 || x >= m || y < 0 || y >= n) continue;\n\n                    const islandId = grid[x][y];\n                    if (islandId > 1 && !set.has(islandId)) {\n                        set.add(islandId);\n                        cur += map[islandId];\n                    }\n                }\n                max = Math.max(max, cur);\n            }\n        }\n    }\n    return max;\n}\n\nfunction dfs(grid, row, col, num) {\n    // bound check\n    if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length) return 0;\n    if (grid[row][col] === 0 || grid[row][col] === num) return 0;\n\n    grid[row][col] = num;\n    return 1 + dfs(grid, row + 1, col, num) + dfs(grid, row - 1, col, num) + \n        dfs(grid, row, col + 1, num) + dfs(grid, row, col - 1, num);\n}\n```
[]