Contain the virus, contain the new coronavirus, fighting...
\n\ndef containVirus(self, grid: List[List[int]]) -> int:\n        m=len(grid)\n        n=len(grid[0])\n        dirs=[(0,1),(1,0),(0,-1),(-1,0)]\n        \n    
\n```\ndef containVirus(self, grid: List[List[int]]) -> int:\n        m=len(grid)\n        n=len(grid[0])\n        dirs=[(0,1),(1,0),(0,-1),(-1,0)]\n        \n        def dfs(x,y,sn):\n            if grid[x][y]==1:\n                v[sn].add((x,y))\n                seen.add((x,y))\n                for dx,dy in dirs:\n                    nx=x+dx\n                    ny=y+dy\n                    if 0<=nx<m and 0<=ny<n:                             \n                        if grid[nx][ny]==0:\n                            f[sn].add((nx,ny))\n                            w[sn]+=1\n                        if grid[nx][ny]==1 and (nx,ny) not in v[sn]:\n                            v[sn].add((nx,ny))\n                            dfs(nx,ny,sn)\n                                   \n        res=0\n        while True:\n            v=collections.defaultdict(set)  # infected area\n            f=collections.defaultdict(set)  # potential risk area\n            w=collections.defaultdict(int)  # walls in need\n            seen=set()\n                        \n            for i in range(m):\n                for j in range(n):\n                    if grid[i][j]==1 and (i,j) not in seen:\n                        dfs(i,j,(i,j))\n             \n            if len(f)==0:   \n                break\n\n            nnfrontiers = sorted(f,key=lambda x:len(f[x]),reverse=True)\n            if len(nnfrontiers)>0:\n                nnfrontier = nnfrontiers[0]                \n                res+=w[nnfrontier]\n\n                for x,y in v[nnfrontier]:   # get isolated\n                    grid[x][y]=2\n                \n                if len(nnfrontiers)>1:      # spread the frontiers\n                    for i in range(1,len(nnfrontiers)):\n                        nnfrontier2 = nnfrontiers[i]\n                        for x,y in f[nnfrontier2]:\n                            grid[x][y]=1\n        return res\n```
[]