Initialize a variable to keep track of the total number of walls built.
While there are infected regions that can spread:
a. Use BFS to find all infected regions and their frontiers (uninfected cells that are adjacent to the infected region).
b. For each region, calculate the perimeter (number of walls needed to quarantine) and the threat level (number of uninfected cells it can infect).
c. Select the most threatening region to quarantine based on the threat level.
d. Build walls around the selected region and update the total number of walls.
e. Spread the virus from the remaining regions by updating the grid.