C++ | Simple DFS Solution
Code

# Code
```cpp []
class Solution {
    int dx[4]={0,1,0,-1};
    int dy[4]={1,0,-1,0};
    int m,n;
    bool isValidMove(int x, int y) {
        return x>=0 && y>=0 && x<m && y<n;
    }
    void dfs(int i,int j, vector<vector<int>>& g, set<pair<int,int>> &st)
    {
        g[i][j]=-1;   // Mark as visited
        //Move to next infected
        for(int k=0;k<4;k++)
        {
            int x=i+dx[k], y=j+dy[k];
            if(isValidMove(x,y)) {
                if(g[x][y]==1)
                    dfs(x,y,g,st);
                else if(g[x][y]==0)
                    st.insert({x,y});        
            }
        }
    }

    int constructWalls(int i,int j, vector<vector<int>>& g)
    {
        g[i][j]=-2;    // Marked as sealed within walls
        int wallsCount=0;
        for(int k=0;k<4;k++)
        {
            int x=i+dx[k], y=j+dy[k];
            if(isValidMove(x,y)) {
                if(g[x][y]==0)
                    wallsCount++; 
                else if(g[x][y]==-1) 
                   wallsCount+= constructWalls(x,y,g);     
            }
        }
        return wallsCount;
    }

    void spreadToNearest(int i,int j, vector<vector<int>>& g)
    {
        g[i][j]=1;    // Marked as infected
        for(int k=0;k<4;k++)
        {
            int x=i+dx[k], y=j+dy[k];
            if(isValidMove(x,y)) {
                if(g[x][y]==0)
                    g[x][y]=1; 
                else if(g[x][y]==-1) 
                   spreadToNearest(x,y,g);     
            }
        }
    }
public:
    int containVirus(vector<vector<int>>& g) 
    {
        m=g.size(),n=g[0].size();
        int totalWalls=0;
        while(true)
        {
            //Find component that threatens the most uninfected cells
            int maxInfected=0,x=0,y=0;
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(g[i][j]==1)
                    {
                        set<pair<int,int>>st;
                        dfs(i,j,g, st);
                        int currInfected=st.size();
                        if(currInfected>maxInfected)
                        {
                            maxInfected=currInfected;
                            x=i,y=j;
                        }
                    }
                }
            }
            if(maxInfected==0)
                break;
            int wallsRequired=constructWalls(x,y,g);
            totalWalls+=wallsRequired;
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++)
                {
                    if(g[i][j]==-1)
                    {
                        spreadToNearest(i,j,g); 
                    }
                }
            }
               
        }
        return totalWalls;
    }
};
```
['C++']