Using BFS
Code\n\nclass Solution:\n    def containVirus(self, isInfected: List[List[int]]) -> int:\n        dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n        m, n = len(
# Code\n```\nclass Solution:\n    def containVirus(self, isInfected: List[List[int]]) -> int:\n        dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n        m, n = len(isInfected), len(isInfected[0])\n        ans = 0\n        while True:\n            # 1. BFS\n            #   neighbors stores every to-spread/to-put-wall area. \n            #   firewalls stores the countings for every area. \n            neighbors, firewalls = list(), list()\n            for i in range(m):\n                for j in range(n):\n                    if isInfected[i][j] == 1:\n                        # detect the infected area \n                        q = deque([(i, j)])\n                        # index of this area\n                        idx = len(neighbors) + 1\n                        # record all cells in this to-spread/to-put-wall area\n                        neighbor = set()\n                        # count walls needed\n                        firewall = 0\n                        print(i,j,idx)\n                        # mark as visited\n                        isInfected[i][j] = -idx\n\n                        while q:\n                            x, y = q.popleft()\n                            for d in range(4):\n                                # detect around\n                                nx, ny = x + dirs[d][0], y + dirs[d][1]\n                                # ensure within the matrix\n                                if 0 <= nx < m and 0 <= ny < n:\n                                    if isInfected[nx][ny] == 1:\n                                        # update infected area\n                                        q.append((nx, ny))\n                                        # mark as visited\n                                        isInfected[nx][ny] = -idx\n                                    elif isInfected[nx][ny] == 0:\n                                        # update counting and neighbor\n                                        firewall += 1\n                                        neighbor.add((nx, ny))\n\n                        \n                        \n                        # update neighbors and firewalls\n                        neighbors.append(neighbor)\n                        firewalls.append(firewall)\n                        print(neighbors)\n                        print(firewalls)\n\n            # 2. none infected areas\n            if not neighbors:\n                break\n            \n            # 3. calculate the largest area:\n            idx = 0\n            for i in range(1, len(neighbors)):\n                if len(neighbors[i]) > len(neighbors[idx]):\n                    idx = i\n                    \n            # 4. update the ans\n            ans += firewalls[idx]\n\n            # if only 1 area in total, this is the final answer\n            if len(neighbors) == 1:\n                # return ans\n                break \n            \n\n            # 5. mark as processed and modify the unprocessed\n            print(idx)\n            print(isInfected)\n            for i in range(m):\n                for j in range(n):\n                    if isInfected[i][j] < 0:\n                        # unprocess\n                        if isInfected[i][j] != - (idx + 1):\n                            isInfected[i][j] = 1\n                        else:\n                            isInfected[i][j] = 2\n            # 6. mark the spreaded\n            for i, neighbor in enumerate(neighbors):\n                if i != idx:\n                    # spread\n                    for x, y in neighbor:\n                        isInfected[x][y] = 1\n            \n            \n\n        return ans\n```
['Python3']