C++ faster than 92%
First get the new infection count for every region without spreading the infection. Also save the infected area cells for spreading the infection later. To coun
First get the new infection count for every region without spreading the infection. Also save the infected area cells for spreading the infection later. To count the possibly infected cells for every region, I am using different area/region code for visited matrix to not skip the non-infected cells. \n\nAfter that get the wall count for maximum infected area and spread infection from other areas.\n\n```\nclass Solution {\n    int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};\n    \n    int getInfectionCount(vector<vector<int>>& grid, vector<vector<int>>& isVis, vector<pair<int, int>>& areaCells, int areaCode, int i, int j) {\n        if (i < 0 or j < 0 or i >= grid.size() or j >= grid[0].size() or isVis[i][j] == areaCode or grid[i][j] == 2) {\n            return 0;\n        }\n\n        isVis[i][j] = areaCode;\n        if (grid[i][j] == 0) {\n            return 1;\n        }\n        \n        areaCells.push_back({i, j});\n        \n        int infectedCellsCount = 0;\n        for (int d = 0; d < 4; d++) {\n            infectedCellsCount += getInfectionCount(grid, isVis, areaCells, areaCode, i + dir[d][0], j + dir[d][1]);\n        }\n        \n        return infectedCellsCount;\n    }\n    \n    int infectAndGetWallCount(vector<vector<int>>& grid, vector<pair<int, int>>& areaCells, bool infect) {\n        int wallCount = 0;\n        \n        for (auto[i, j] : areaCells) {\n            if (!infect) {\n                grid[i][j] = 2;\n            }\n            for (int d = 0; d < 4; d++) {\n                int ii = i + dir[d][0];\n                int jj = j + dir[d][1];\n                \n                if (ii < 0 or jj < 0 or ii >= grid.size() or jj >= grid[0].size()) continue;\n                \n                if (grid[ii][jj] == 0) {\n                    if (infect) {\n                        grid[ii][jj] = 1;\n                    }\n                    else {\n                        wallCount++;\n                    }\n                }\n            }\n        }\n        \n        return wallCount;\n    }\n    \npublic:\n    int containVirus(vector<vector<int>>& isInfected) {\n        int m = isInfected.size(), n = isInfected[0].size(), totalWalls = 0;\n        \n        while (true) {\n            int maxInfectionCount = 0, maxArea = -1;\n            vector<vector<pair<int, int>>> areas;\n            vector<vector<int>> isVis(m, vector<int>(n, 0));\n            int areaCode = 1;\n\n            for (int i = 0; i < m; i++) { \n                for (int j = 0; j < n; j++) {\n                    if (isInfected[i][j] == 1 and isVis[i][j] == 0) {\n                        vector<pair<int, int>> areaCells;\n                        int infectionCount = getInfectionCount(isInfected, isVis, areaCells, areaCode, i, j);\n                        areaCode++;\n                        \n                        if (infectionCount > maxInfectionCount) {\n                            maxInfectionCount = infectionCount;\n                            maxArea = areas.size();\n                        }\n                        areas.push_back(areaCells);\n                    }\n                }\n            }\n            \n            if (maxArea == -1) break;\n            \n            totalWalls += infectAndGetWallCount(isInfected, areas[maxArea], false);\n            for (int i = 0; i < areas.size(); i++) {\n                if (i == maxArea) continue;\n                infectAndGetWallCount(isInfected, areas[i], true);\n            }\n        }\n        \n        return totalWalls;\n    }\n};\n```
['Depth-First Search']