C++ Easy Solution
Code


# Code
```cpp []
class Solution {
public:
    vector<int> del = {-1, 0, 1, 0};

    void dfs(int i, int j, vector<vector<int>>& isInfected, vector<vector<int>>& vis, vector<int>& eachComp, int n, int m) {
        vis[i][j] = 1;
        int node = i * m + j;
        eachComp.push_back(node);
        for (int k = 0; k < 4; k++) {
            int r = i + del[k];
            int c = j + del[(k + 1) % 4];
            if (r >= 0 && c >= 0 && r < n && c < m && isInfected[r][c] == 1 && !vis[r][c]) {
                dfs(r, c, isInfected, vis, eachComp, n, m);
            }
        }
    }

    int getCountOfNeigh(vector<int>& component, int n, int m, vector<vector<int>>& isInfected) {
        int count = 0;
        set<pair<int, int>> uniqueNeigh;
        for (int node : component) {
            int r = node / m;
            int c = node % m;
            for (int k = 0; k < 4; k++) {
                int nr = r + del[k];
                int nc = c + del[(k + 1) % 4];
                if (nr >= 0 && nc >= 0 && nr < n && nc < m && isInfected[nr][nc] == 0) {
                    uniqueNeigh.insert({nr, nc});
                }
            }
        }
        return uniqueNeigh.size();
    }

    void increaseTheDecease(vector<int>& component, int n, int m, vector<vector<int>>& isInfected) {
        vector<int> newComp = component;
        for (int node : component) {
            int r = node / m;
            int c = node % m;
            for (int k = 0; k < 4; k++) {
                int nr = r + del[k];
                int nc = c + del[(k + 1) % 4];
                if (nr >= 0 && nc >= 0 && nr < n && nc < m && isInfected[nr][nc] == 0) {
                    isInfected[nr][nc] = 1;
                    newComp.push_back(nr * m + nc);
                }
            }
        }
        component = newComp;
    }

    int getCountOfWalls(vector<int>& component, int n, int m, vector<vector<int>>& isInfected) {
        int walls = 0;
        for (int node : component) {
            int r = node / m;
            int c = node % m;
            for (int k = 0; k < 4; k++) {
                int nr = r + del[k];
                int nc = c + del[(k + 1) % 4];
                if (nr >= 0 && nc >= 0 && nr < n && nc < m && isInfected[nr][nc] == 0) {
                    walls++;
                }
            }
        }
        return walls;
    }

    int containVirus(vector<vector<int>>& isInfected) {
        int n = isInfected.size(), m = isInfected[0].size();
        int totalWalls = 0;
        
        while (true) {
            vector<vector<int>> components;
            vector<vector<int>> vis(n, vector<int>(m, 0));

            // Find all infected components
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (!vis[i][j] && isInfected[i][j] == 1) {
                        vector<int> eachComp;
                        dfs(i, j, isInfected, vis, eachComp, n, m);
                        components.push_back(eachComp);
                    }
                }
            }

            if (components.empty()) break;

            // Choose the component with the maximum number of neighbors
            int maxNeigh = -1, chosenIdx = -1;
            for (int i = 0; i < components.size(); i++) {
                int count = getCountOfNeigh(components[i], n, m, isInfected);
                if (count > maxNeigh) {
                    maxNeigh = count;
                    chosenIdx = i;
                }
            }

            if (maxNeigh == 0) break; // No spread possible

            // Contain the most dangerous region
            totalWalls += getCountOfWalls(components[chosenIdx], n, m, isInfected);
            for (int node : components[chosenIdx]) {
                int r = node / m, c = node % m;
                isInfected[r][c] = -1; // Mark as contained
            }

            // Spread the remaining components
            for (int i = 0; i < components.size(); i++) {
                if (i != chosenIdx) {
                    increaseTheDecease(components[i], n, m, isInfected);
                }
            }
        }
        return totalWalls;
    }
};

```
['Depth-First Search', 'Matrix', 'C++']