Pyhon Easy Solution || DFS || Time O(m*n*max(m , n)) || Faster
\n class Solution:\n    def containVirus(self, mat: List[List[int]]) -> int:\n        m,n = len(mat),len(mat[0])\n\n        def dfs(i,j,visited,nextInfected):  
```\n class Solution:\n    def containVirus(self, mat: List[List[int]]) -> int:\n        m,n = len(mat),len(mat[0])\n\n        def dfs(i,j,visited,nextInfected):  # return no. of walls require to quarantined dfs area\n            if 0<=i<m and 0<=j<n and (i,j) not in visited:\n                if mat[i][j]==2: # Already quarantined cell\n                    return 0\n                if mat[i][j]==0:\n                    nextInfected.add((i,j)) # add cell which will be infected next day\n                    return 1   # require one wall to quarantined cell from one side\n                    \n                else:\n                    visited.add((i,j))\n                    return dfs(i-1,j,visited,nextInfected) + dfs(i+1,j,visited,nextInfected) + dfs(i,j-1,visited,nextInfected) + dfs(i,j+1,visited,nextInfected)  # traverse all four direction\n            else:\n                return 0\n\t\t\t\t\n        ans = 0 \n        while True:   # this loop running "how many days we should installing the walls" times\n            # For every day check which area infect more cells\n            visited = set()  # Using in dfs\n            All_nextinfect  = set()\n            stop , walls = set(),0   # here stop store the indices of maximum no. of cells in which we stop spreading of virus this day\n            \n            for i in range(m):\n                for j in range(n):\n                    if mat[i][j]==1 and (i,j) not in visited:\n                        nextInfected = set()\n                        a = dfs(i,j,visited,nextInfected)\n                        \n                        if len(stop)<len(nextInfected):\n                            All_nextinfect  = All_nextinfect | stop # leave previous saved area from virus\n                            stop = nextInfected  # pick new area which we want to save\n                            walls = a  # require walls\n                            p,q = i,j  # starting position(indices) of this area\n                        else:\n                            All_nextinfect  = All_nextinfect | nextInfected   \n                            \n            if not stop : # if our job is done i.e. No cell will be infect Later\n                break\n            ans += walls  # add new walls installed this day\n            \n            # change each cell value to 2 which will be covered by quarantined area\n            def fun(p,q):\n                if 0<=p<m and 0<=q<n and mat[p][q]==1:\n                    mat[p][q]=2\n                    fun(p+1,q)\n                    fun(p-1,q)\n                    fun(p,q-1)\n                    fun(p,q+1)\n            fun(p,q) # start dfs from start point of quarantined area\n            \n            for a,b in All_nextinfect: # set new infected cell value = 1 for iterating next day\n                mat[a][b] = 1\n\n        return ans   # Final answer \n```\t\t\n\t\t\n\t\t\n**if you like the solution : please  Upvote    :)**\n\t\t\n\t\t\n\t\t\n\t\t\n \n
['Depth-First Search', 'Python', 'Python3']