Swift - Contain Virus
\n\nclass Solution {\n    func containVirus(_ isInfected: [[Int]]) -> Int {\n        // Copy of the original grid to modify during processing\n        var grid 
\n```\nclass Solution {\n    func containVirus(_ isInfected: [[Int]]) -> Int {\n        // Copy of the original grid to modify during processing\n        var grid = isInfected\n        // Directions for moving up, down, left, and right\n        let directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]\n        // Variable to keep track of the total number of walls used\n        var totalWalls = 0\n        \n        // Depth-First Search (DFS) function to explore a viral region\n        func dfs(_ i: Int, _ j: Int, _ region: inout Set<[Int]>, _ frontier: inout Set<[Int]>, _ walls: inout Int) {\n            let n = grid.count\n            let m = grid[0].count\n            // Stack for DFS traversal\n            var stack = [(i, j)]\n            region.insert([i, j])\n            var idx = 0\n            \n            while idx < stack.count {\n                let (x, y) = stack[idx]\n                idx += 1\n                \n                for direction in directions {\n                    let newX = x + direction.0\n                    let newY = y + direction.1\n                    \n                    // Check if the new position is within bounds\n                    if newX >= 0 && newX < n && newY >= 0 && newY < m {\n                        if grid[newX][newY] == 1 && !region.contains([newX, newY]) {\n                            // If the new cell is infected and not yet visited in this region\n                            stack.append((newX, newY))\n                            region.insert([newX, newY])\n                        } else if grid[newX][newY] == 0 {\n                            // If the new cell is uninfected (part of the frontier)\n                            frontier.insert([newX, newY])\n                            walls += 1\n                        }\n                    }\n                }\n            }\n        }\n        \n        // Main loop to process the grid until no more infections can spread\n        while true {\n            var regions = [Set<[Int]>]()\n            var frontiers = [Set<[Int]>]()\n            var wallsList = [Int]()\n            \n            let n = grid.count\n            let m = grid[0].count\n            var visited = Set<[Int]>()\n            \n            // Identify all viral regions and their frontiers\n            for i in 0..<n {\n                for j in 0..<m {\n                    if grid[i][j] == 1 && !visited.contains([i, j]) {\n                        var region = Set<[Int]>()\n                        var frontier = Set<[Int]>()\n                        var walls = 0\n                        dfs(i, j, &region, &frontier, &walls)\n                        for cell in region {\n                            visited.insert(cell)\n                        }\n                        regions.append(region)\n                        frontiers.append(frontier)\n                        wallsList.append(walls)\n                    }\n                }\n            }\n            \n            // If there are no more viral regions, break the loop\n            if regions.isEmpty {\n                break\n            }\n            \n            // Find the region with the largest frontier\n            var maxFrontierIdx = 0\n            for i in 1..<frontiers.count {\n                if frontiers[i].count > frontiers[maxFrontierIdx].count {\n                    maxFrontierIdx = i\n                }\n            }\n            \n            // Add the walls needed for the most dangerous region to the total count\n            totalWalls += wallsList[maxFrontierIdx]\n            \n            // Quarantine the most dangerous region by marking its cells with -1\n            for cell in regions[maxFrontierIdx] {\n                grid[cell[0]][cell[1]] = -1\n            }\n            \n            // Spread the virus in the remaining regions\n            for i in 0..<regions.count {\n                if i != maxFrontierIdx {\n                    for cell in frontiers[i] {\n                        grid[cell[0]][cell[1]] = 1\n                    }\n                }\n            }\n        }\n        \n        return totalWalls\n    }\n}\n\n```
['Swift']