Explained solution 4ms
Code
![image.png](https://assets.leetcode.com/users/images/bfda097f-f02c-48dd-97c6-b11ab2ca9158_1743081265.7291005.png)



# Code
```cpp []
class Solution {
    int m,n;
public:
    void spread_to_neighbors(vector<vector<int>>& board, int r, int c,int prev_marker, int marker){
        // skip out of border and blocked cells
        if (r < 0 || c < 0 || r>=m || c >=n || board[r][c] == INT_MAX ) 
            return;

        bool is_infected = board[r][c] == prev_marker;
        board[r][c] = marker;

        if (is_infected){
            spread_to_neighbors(board,r,c+1,prev_marker,marker);
            spread_to_neighbors(board,r,c-1,prev_marker,marker);
            spread_to_neighbors(board,r+1,c,prev_marker,marker);
            spread_to_neighbors(board,r-1,c,prev_marker,marker);
        }
        
    }

    void fill_area(vector<vector<int>>& board, int r, int c,int marker, int& walls, int& num_adj){
        // skip out of border, visited, and blocked cells
        if (r < 0 || c < 0 || r>=m || c >=n || board[r][c] == marker || board[r][c] == INT_MAX ) 
            return;
        // empty neighbor cell
        if (board[r][c] <= 0){
            walls++;
            // add adjacent empty cell if visited the first time
            if (board[r][c] != -marker) 
                num_adj++;
            board[r][c] = -marker;
            return;
        }
        board[r][c] = marker;
        fill_area(board,r,c+1,marker,walls,num_adj);
        fill_area(board,r,c-1,marker,walls,num_adj);
        fill_area(board,r+1,c,marker,walls,num_adj);
        fill_area(board,r-1,c,marker,walls,num_adj);
    }
    int containVirus(vector<vector<int>>& board) {
        int total_walls = 0;
        m = size(board), n = size(board[0]);

        unordered_map<int, pair<int,int>> infected_areas,prev_infected;


        // used for infected areas marking
        int marker = 2;
        int walls, num_adj;

        // at the beginning all cells are need to be checked if infected
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                prev_infected[n*i+j] = {i,j};
            }
        }


        while(true){
            // [num_adj, num_walls, x, y]
            vector<int> area_for_del({0,0,0,0});

            for(auto [_, pos] : prev_infected){
                    auto [i,j] = pos;
                    int v = board[i][j];
                    // marking each infected area by a different positive integer
                    if (v > 0 && infected_areas.count(v)==0){
                        walls = 0;
                        num_adj = 0;
                        fill_area(board,i,j,marker,walls,num_adj);
                        infected_areas[marker] = {i,j};
                        if (num_adj > area_for_del[0])
                            area_for_del = {num_adj, walls, i ,j};
                        marker++;
                    }
                
            }
            // stop if all area is infected or quarantined
            if (area_for_del[0] == 0) break;

            // block infected area with most neighbors
            total_walls += area_for_del[1];
            fill_area(board,area_for_del[2],area_for_del[3],INT_MAX,walls,num_adj);

            // spread to neighbors
            for (auto& [used_marker, pos] :infected_areas){
                spread_to_neighbors(board, pos.first, pos.second, used_marker, marker);
                marker++;
            }
            swap(prev_infected, infected_areas);
            infected_areas.clear();
        }
        return total_walls;

    }
}; 
```
['C++']