1254. Number of Closed Islands (Medium) - TengnanYao/daily_leetcode GitHub Wiki

/**
 * @param {number[][]} grid
 * @return {number}
 */
var closedIsland = function(grid) {
    const dfs = (i, j) => {
        grid[i][j] = 1
        for (const [dx, dy] of [[0, 1], [1, 0], [0, -1], [-1, 0]]) {
            const [x, y] = [i + dx, j + dy]
            if (x < 0 || x >= m || y < 0 || y >= n) {
                res = false
            } else {
                if (grid[x][y] === 0) {
                    dfs(x, y)
                }
            }
        }
    }
    const [m, n] = [grid.length, grid[0].length]
    let result = 0, res
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < n; j++) {
            if (grid[i][j] === 0) {
                res = true
                dfs(i, j)
                result += res
            }
        }
    }
    return result
};
⚠️ **GitHub.com Fallback** ⚠️