Python Short and Easy to Understand Solution
\n\nclass Solution:\n    def containVirus(self, isInfected: List[List[int]]) -> int:\n        walls_needed = 0\n\n        # identify infected and uninfected cel
\n```\nclass Solution:\n    def containVirus(self, isInfected: List[List[int]]) -> int:\n        walls_needed = 0\n\n        # identify infected and uninfected cells\n        infected = [(i, j) for i in range(len(isInfected)) for j in range(len(isInfected[0])) if isInfected[i][j] == 1]\n        uninfected = [(i, j) for i in range(len(isInfected)) for j in range(len(isInfected[0])) if isInfected[i][j] != 1]\n\n        # group infected cells\n        def grp_infected(infected):\n            grp = []\n            while infected:\n                cur = [infected.pop()]\n                cur_grp = [cur[0]]\n                while cur:\n                    cur_dot = cur.pop()\n                    south = (cur_dot[0] - 1, cur_dot[1]); north = (cur_dot[0] + 1, cur_dot[1])\n                    west = (cur_dot[0], cur_dot[1] - 1); east = (cur_dot[0], cur_dot[1] + 1)\n                    for i in [south, north, west, east]:\n                        if i in infected:\n                            cur_grp.append(i)\n                            cur.append(i)\n                            infected.remove(i)\n                grp.append(cur_grp)\n            return grp\n        grp = grp_infected(infected)\n\n        # identify next-to-be infected cells and select the correct group to quarantine\n        while grp and uninfected:\n            next_grp = []\n            for i in grp:\n                next_grp_temp = []\n                for cur_dot in i:\n                    south = (cur_dot[0] - 1, cur_dot[1]); north = (cur_dot[0] + 1, cur_dot[1])\n                    west = (cur_dot[0], cur_dot[1] - 1); east = (cur_dot[0], cur_dot[1] + 1)\n                    for k in [south, north, west, east]:\n                        if k in uninfected:\n                            next_grp_temp.append(k)\n                next_grp.append(next_grp_temp)\n\n            max_infected = [len(set(i)) for i in next_grp]\n            idx = max_infected.index(max(max_infected))\n            walls_needed += len(next_grp[idx])\n\n            del next_grp[idx]\n            del grp[idx]\n            grp = [i + j for i, j in zip(next_grp, grp)]\n            next_grp = list(set(j for i in next_grp for j in i))\n            for i in next_grp:\n                uninfected.remove(i)\n            grp = grp_infected(list(set(j for i in grp for j in i)))\n        return walls_needed\n```
['Python3']