<p>A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls.</p>

<p>The world is modeled as an <code>m x n</code> binary grid <code>isInfected</code>, where <code>isInfected[i][j] == 0</code> represents uninfected cells, and <code>isInfected[i][j] == 1</code> represents cells contaminated with the virus. A wall (and only one wall) can be installed between any two <strong>4-directionally</strong> adjacent cells, on the shared boundary.</p>

<p>Every night, the virus spreads to all neighboring cells in all four directions unless blocked by a wall. Resources are limited. Each day, you can install walls around only one region (i.e., the affected area (continuous block of infected cells) that threatens the most uninfected cells the following night). There <strong>will never be a tie</strong>.</p>

<p>Return <em>the number of walls used to quarantine all the infected regions</em>. If the world will become fully infected, return the number of walls used.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/01/virus11-grid.jpg" style="width: 500px; height: 255px;" />
<pre>
<strong>Input:</strong> isInfected = [[0,1,0,0,0,0,0,1],[0,1,0,0,0,0,0,1],[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0]]
<strong>Output:</strong> 10
<strong>Explanation:</strong> There are 2 contaminated regions.
On the first day, add 5 walls to quarantine the viral region on the left. The board after the virus spreads is:
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/01/virus12edited-grid.jpg" style="width: 500px; height: 257px;" />
On the second day, add 5 walls to quarantine the viral region on the right. The virus is fully contained.
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/01/virus13edited-grid.jpg" style="width: 500px; height: 261px;" />
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/06/01/virus2-grid.jpg" style="width: 653px; height: 253px;" />
<pre>
<strong>Input:</strong> isInfected = [[1,1,1],[1,0,1],[1,1,1]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Even though there is only one cell saved, there are 4 walls built.
Notice that walls are only built on the shared boundary of two different cells.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> isInfected = [[1,1,1,0,0,0,0,0,0],[1,0,1,0,1,1,1,1,1],[1,1,1,0,0,0,0,0,0]]
<strong>Output:</strong> 13
<strong>Explanation:</strong> The region on the left only builds two new walls.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m ==&nbsp;isInfected.length</code></li>
	<li><code>n ==&nbsp;isInfected[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 50</code></li>
	<li><code>isInfected[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
	<li>There is always a contiguous viral region throughout the described process that will <strong>infect strictly more uncontaminated squares</strong> in the next round.</li>
</ul>

class Solution {
    public:
        vector<vector<int> > g;
            int n, m, c, mx, w, r, ans, itr;
                unordered_set<int> s;
                    
                        int dfs(int i, int j){
                                if(i<0 || i>=n || j<0 || j>=m || g[i][j]!=1)
                                            return 0;
                                                    int ans=0;
                                                            if(i+1<n && g[i+1][j]==0){
                                                                        s.insert((i+1)*m+j);
                                                                                    ans++;
                                                                                            }
                                                                                                    if(i-1>=0 && g[i-1][j]==0){
                                                                                                                s.insert((i-1)*m+j);
                                                                                                                            ans++;
                                                                                                                                    }
                                                                                                                                            if(j+1<m && g[i][j+1]==0){
                                                                                                                                                        s.insert(i*m+(j+1));
                                                                                                                                                                    ans++;
                                                                                                                                                                            }
                                                                                                                                                                                    if(j-1>=0 && g[i][j-1]==0){
                                                                                                                                                                                                s.insert(i*m+(j-1));
                                                                                                                                                                                                            ans++;
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                            g[i][j]=c;
                                                                                                                                                                                                                                    ans+=dfs(i+1, j);
                                                                                                                                                                                                                                            ans+=dfs(i-1, j);
                                                                                                                                                                                                                                                    ans+=dfs(i, j+1);
                                                                                                                                                                                                                                                            ans+=dfs(i, j-1);
                                                                                                                                                                                                                                                                    return ans; // total number of walls needed to block this connected component
                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                int containVirus(vector<vector<int>>& grid) {
                                                                                                                                                                                                                                                                                        g=grid, n=g.size(), m=g[0].size(), ans=0;
                                                                                                                                                                                                                                                                                                while(true){
                                                                                                                                                                                                                                                                                                            c=2, mx=0;
                                                                                                                                                                                                                                                                                                                        for(int i=0; i<n; i++){
                                                                                                                                                                                                                                                                                                                                        for(int j=0; j<m; j++){
                                                                                                                                                                                                                                                                                                                                                            if(g[i][j]==1){
                                                                                                                                                                                                                                                                                                                                                                                    s.clear();
                                                                                                                                                                                                                                                                                                                                                                                                            int walls=dfs(i, j);
                                                                                                                                                                                                                                                                                                                                                                                                                                    if(mx<s.size()){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                mx=s.size();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            w=walls;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        r=c;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        c++;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if(mx==0)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    break;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ans+=w;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            for(int i=0; i<n; i++){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            for(int j=0; j<m; j++){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                if(g[i][j]==r)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        g[i][j]=1e9;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            else if(g[i][j]>1 && g[i][j]!=1e9){
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    g[i][j]=1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if(i+1<n && !g[i+1][j]) g[i+1][j]=1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if(i-1>=0 && !g[i-1][j]) g[i-1][j]=1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            if(j+1<m && !g[i][j+1]) g[i][j+1]=1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    if(j-1>=0 && !g[i][j-1]) g[i][j-1]=1;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    return ans;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        };

19500