JavaScript | Simple Solution Written for Clarity
# Intuition\nIn order to solve the problem, we are going to simulate the exact process. In code, the idea\'s as simple as writing this loop:\n```javascript\nvar result = 0;\n\nlet tmp = 0;\n\ndo {\n    tmp = contain(isInfected);\n    result += tmp;\n    spread(isInfected);\n} while (tmp != 0);\n\nreturn result;\n```\nThis loop simulates a day-to-day process. Each day, we contain the most dangrous region, then we spread the remaining regions\n\n# Approach\nLet\'s first understand the methods being used:\n- `contain(board)`: Finds the most dangrous region on the board and secures it by replacing each cell with `2`, indicating that the cell is fenced. Returns the number of walls used to secure the area.\n  - `countCells(board, i, j)`: Finds the number of healthy cells that would be infected by the given region if we do nothing about it. It uses DFS to find the number of cells that would be infected by given coordinates.\n  - `countWalls(board, i, j)`: Similar to `countCells`, but it finds the number of walls required to secure an area.\n  - `secure(board, i, j)`: Secure the given region by replacing each infected cell with `2`.\n- `spread(board)`: Spreads each unfenced region on the board towards its neighbors.\nWe can now use these functions to solve the problem. We are going to use `contain` on each iterations to contain the most dangrous region and find the number of walls used. If there are no regions to secure, the game is over and we return the number of walls used so far, otherwise, we spread the remaining regions.\n\n```javascript\nvar result = 0; // Total number of walls used\n\nlet tmp = 0; // Counter variable keeping track of current number of walls\n\ndo {\n    tmp = contain(isInfected); // Find the number of walls used\n    result += tmp;\n    spread(isInfected); // Spreads the board if there are any uncontained regions\n} while (tmp != 0);\n\nreturn result; // If no walls were used in the last iteration, the game is over and we return the total number of walls\n```\n\nThe remaining functions simply use DFS and follows a similar pattern. In fact, I could have written a single DFS function to compute all of this, but I wanted to make the solution more clearer.\n\n# Code\n```javascript []\n/**\n * @param {number[][]} isInfected\n * @return {number}\n */\nvar containVirus = function(isInfected) {\n    // Find the most dangrous region\n    var result = 0;\n\n    let tmp = 0;\n\n    do {\n        tmp = contain(isInfected);\n        result += tmp;\n        spread(isInfected);\n    } while (tmp != 0);\n\n    return result;\n};\n\nfunction contain(board) {\n    let max = 0;\n    let x = -1;\n    let y = -1;\n\n    var visited = {};\n\n    for (let i = 0; i < board.length; i++) {\n        for (let j = 0; j < board[i].length; j++) {\n            if (board[i][j] == 1 && !visited[i + \'_\' + j]) {\n                let walls = countCells(board, i, j, visited);\n                if (walls > max) {\n                    max = walls;\n                    x = i;\n                    y = j;\n                }\n            }\n        }\n    }\n\n    if (x >= 0 && y >= 0) {\n        max = countWalls(board, x, y, {});\n        secure(board, x, y);\n    }\n\n    return max;\n}\n\nfunction countCells(board, i, j, infVisited) {\n    var result = 0;\n\n    let stack = [[i, j]];\n\n    var visited = {};\n\n    let neighbors = [\n        [-1, 0],\n        [1, 0],\n        [0, -1],\n        [0, 1],\n    ];\n\n    visited[i + \'_\' + j] = true;\n\n    while (stack.length > 0) {\n        let node = stack.pop();\n\n        for (let n of neighbors) {\n            let x = node[0] + n[0];\n            let y = node[1] + n[1];\n\n            if (x >= 0 && y >= 0 && x < board.length && y < board[x].length) {\n                if (board[x][y] == 0) {\n                    if (visited[x + "_" + y]) continue;\n\n                    visited[x + "_" + y] = true;\n                    result++;\n                }\n                else if (board[x][y] == 1) {\n                    if (visited[x + "_" + y]) continue;\n\n                    visited[x + "_" + y] = true;\n                    infVisited[x + "_" + y] = true;\n                    stack.push([x, y]);\n                }\n            }\n        }\n    }\n\n    return result;\n}\n\nfunction countWalls(board, i, j, visited) {\n    var result = 0;\n\n    let stack = [[i, j]];\n\n    let neighbors = [\n        [-1, 0],\n        [1, 0],\n        [0, -1],\n        [0, 1],\n    ];\n\n    visited[i + \'_\' + j] = true;\n\n    while (stack.length > 0) {\n        let node = stack.pop();\n\n        for (let n of neighbors) {\n            let x = node[0] + n[0];\n            let y = node[1] + n[1];\n\n            if (x >= 0 && y >= 0 && x < board.length && y < board[x].length) {\n                if (board[x][y] == 0) {\n                    result++;\n                }\n                else if (board[x][y] == 1) {\n                    if (visited[x + "_" + y]) continue;\n\n\n                    visited[x + "_" + y] = true;\n                    stack.push([x, y]);\n                }\n            }\n        }\n    }\n\n    return result;\n}\n\nfunction secure(board, i, j) {\n    let stack = [[i, j]];\n\n    let neighbors = [\n        [-1, 0],\n        [1, 0],\n        [0, -1],\n        [0, 1],\n    ];\n\n    while (stack.length > 0) {\n        let node = stack.pop();\n\n        board[node[0]][node[1]] = 2;\n\n        for (let n of neighbors) {\n            let x = node[0] + n[0];\n            let y = node[1] + n[1];\n\n            if (x >= 0 && y >= 0 && x < board.length && y < board[x].length) {\n                if (board[x][y] == 1) {\n                    stack.push([x, y]);\n                }\n            }\n        }\n    }\n}\n\nfunction spread(board) {\n    var visited = {};\n\n    for (let i = 0; i < board.length; i++) {\n        for (let j = 0; j < board[i].length; j++) {\n            if (board[i][j] == 1 && !visited[i + \'_\' + j]) {\n                spreadRegion(board, i, j, visited);\n            }\n        }\n    }\n\n    for (let row of board) {\n        for (let j = 0; j < row.length; j++) {\n            if (row[j] == 3) row[j] = 1;\n        }\n    }\n}\n\nfunction spreadRegion(board, i, j, visited) {\n    let stack = [[i, j]];\n\n    let neighbors = [\n        [-1, 0],\n        [1, 0],\n        [0, -1],\n        [0, 1],\n    ];\n\n    visited[i + \'_\' + j] = true;\n\n    while (stack.length > 0) {\n        let node = stack.pop();\n\n        for (let n of neighbors) {\n            let x = node[0] + n[0];\n            let y = node[1] + n[1];\n\n            if (x >= 0 && y >= 0 && x < board.length && y < board[x].length) {\n                if (board[x][y] == 1) {\n                    if (visited[x + \'_\' + y]) continue;\n                    visited[x + \'_\' + y] = true;\n\n                    stack.push([x, y]);\n                }\n                else if (board[x][y] == 0) board[x][y] = 3;\n            }\n        }\n    }\n}\n```
['Depth-First Search', 'Graph', 'Matrix', 'JavaScript']