<p>You are given an <code>m x n</code> binary grid <code>grid</code> where <code>1</code> represents land and <code>0</code> represents water. An <strong>island</strong> is a maximal <strong>4-directionally</strong> (horizontal or vertical) connected group of <code>1</code>&#39;s.</p>

<p>The grid is said to be <strong>connected</strong> if we have <strong>exactly one island</strong>, otherwise is said <strong>disconnected</strong>.</p>

<p>In one day, we are allowed to change <strong>any </strong>single land cell <code>(1)</code> into a water cell <code>(0)</code>.</p>

<p>Return <em>the minimum number of days to disconnect the grid</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land1.jpg" style="width: 500px; height: 169px;" />
<pre>
<strong>Input:</strong> grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]

<strong>Output:</strong> 2
<strong>Explanation:</strong> We need at least 2 days to get a disconnected grid.
Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/24/land2.jpg" style="width: 404px; height: 85px;" />
<pre>
<strong>Input:</strong> grid = [[1,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> Grid of full water is also disconnected ([[1,1]] -&gt; [[0,0]]), 0 islands.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution:
    def minDays(self, grid: List[List[int]]) -> int:
        
        m = len(grid)
        n = len(grid[0])
        k = sum(sum(g) for g in grid)
        
        #print(k)
        
        if k == 0:
            return 0
        if k == 1:
            return 1

        first_visited = dict()
        has_articulation_point = False
        lo = dict()
        
        def x(d):
            for i in range(m):
                for j in range(n):
                    if (i, j) in d:
                        print("%3d" % d[(i, j)],end=' ')
                    else:
                        print("   ", end=' ')
                print()
            print()

        def y(d):
            for i in range(m):
                for j in range(n):
                    print("%3d" % d[i][j],end=' ')
                print()
            print()

        #y(grid)    

        def dfs(i, j, prev_i, prev_j, dep):
            nonlocal has_articulation_point
            first_visited[(i, j)] = dep
            lo[(i, j)] = dep
            #print('=======', i, j, dep)
            #x(lo)
            num_dfs_children = 0
            for ii, jj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:
                if (ii, jj) != (prev_i, prev_j) and 0 <= ii < m and 0 <= jj < n:
                    if grid[ii][jj]:
                        if (ii, jj) not in first_visited:
                            dfs(ii, jj, i, j, dep + 1)
                            lo[(i, j)] = min(lo[(i, j)], lo[(ii, jj)])
                            num_dfs_children += 1
                            if dep and lo[(ii, jj)] >= dep:
                                has_articulation_point = True
                        else:
                            lo[(i, j)] = min(lo[(i, j)], first_visited[(ii, jj)])
            if not dep:
                has_articulation_point = has_articulation_point or num_dfs_children > 1

            return

        
        for i in range(m):
            for j in range(n):
                if grid[i][j]:
                    dfs(i, j, -13, -13, 0)
                    
                    #x(first_visited)
                    #x(lo)

                    if len(first_visited) < k:
                        return 0
                    if k == 2:
                        #print("aa")
                        return 2
                    if has_articulation_point:
                        #print("bb")
                        return 1
                    #print("cc")
                    return 2



17800