C++ DFS solution, Easy Understand.
```c++\nclass Solution {\n    \n    int M, N;\n    \n    //marking those cells as -1\n    void buildWall(vector>& fec, int r, int c){\n        //if it is alread
```c++\nclass Solution {\n    \n    int M, N;\n    \n    //marking those cells as -1\n    void buildWall(vector<vector<int>>& fec, int r, int c){\n        //if it is already marked or empty, just return.\n        if(r < 0 || c < 0 || r >= M || c >= N || fec[r][c] <= 0) return;\n        fec[r][c] = -1;//mark this cell.\n        buildWall(fec, r-1, c);\n        buildWall(fec, r, c-1);\n        buildWall(fec, r+1, c);\n        buildWall(fec, r, c+1);\n        return;\n    }\n    //the returning parameter is the number of walls you need to build.\n    int calcWalls(vector<vector<int>>& fec, vector<vector<bool>>& vis, int r, int c, unordered_set<int>& ca){\n        if(r < 0 || c < 0 || r >= fec.size() || c >= fec[0].size() || vis[r][c] || fec[r][c] < 0) return 0;\n        if(fec[r][c] == 0){\n            ca.insert(r*N+c);//a possible expand position\n            return 1;//you need to build a wall here\n        }\n        vis[r][c] = true;\n        return calcWalls(fec, vis, r-1, c, ca) + calcWalls(fec, vis, r+1, c, ca) + calcWalls(fec, vis, r, c-1, ca) + \n            calcWalls(fec, vis, r, c+1, ca);\n    }\n    \npublic:\n    int containVirus(vector<vector<int>>& isInfected) {\n        this->M = isInfected.size(), this->N = isInfected[0].size();\n        int res = 0, indx = 0;//indx is a candidate block position.\n        while(indx != -1){\n            vector<vector<bool>> vis(M, vector<bool>(N));\n            int needbuildwalls = 0, msize = 0;\n            indx = -1;\n            unordered_map<int, unordered_set<int>> conta;//for storing the threatened cells for (i,j) = i*N+j\n            for(int i = 0;i < M;++i ){\n                for(int j = 0;j < N;++j ){\n                    if(!vis[i][j] && isInfected[i][j] == 1){\n                        int tmp = calcWalls(isInfected, vis, i, j, conta[i*N+j]);\n                        if(conta[i*N+j].size() > msize){//updating the candidate position.\n                            needbuildwalls = tmp;\n                            msize = conta[i*N+j].size();\n                            indx = i*N+j;\n                        }\n                    }\n                }//for\n            }//for\n            res += needbuildwalls;\n            if(indx >= 0) buildWall(isInfected, indx/N, indx%N);\n            //the virus spreads to all neighboring cells\n            for(auto iter = conta.begin();iter != conta.end();++iter){\n                if(iter->first != indx){\n                    for(int pos:iter->second){\n                        isInfected[pos/N][pos%N] = 1;//marked as 1\n                    }\n                }\n            }\n        }//while\n        return res;\n    }\n};\n
[]