<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])
        for r in range(m):
            grid[r].append(0)
        grid.append([0] * (n + 1))


        def adj(r, c):
            for dr, dc in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                nr, nc = r + dr, c + dc
                if 0 <= nr < m and 0 <= nc < n and grid[nr][nc] == 1:
                    yield (nr, nc)
        
        def bfs(r, c):
            visited = {(r, c)}
            q = deque([(r, c)])
            while q:
                r, c = q.popleft()
                for nr, nc in adj(r, c):
                    if (nr, nc) in visited:
                        continue
                    visited.add((nr, nc))
                    q.append((nr, nc))
            return visited
        
        islands = []

        visited = [[False] * n for _ in range(m)]
        for r in range(m):
            for c in range(n):
                if grid[r][c] == 0:
                    continue
                if visited[r][c]:
                    continue
                
                cur_island = []
                q = deque([(r, c)])
                visited[r][c] = True
                while q:
                    cr, cc = q.popleft()
                    cur_island.append((cr, cc))
                    for nr, nc in adj(cr, cc):
                        if grid[nr][nc] == 0:
                            continue
                        if visited[nr][nc]:
                            continue
                        visited[nr][nc] = True
                        q.append((nr, nc))
                islands.append(cur_island)

        if len(islands) != 1:
            return 0
        
        total_lands = 0
        max_keep = 0
        for island in islands:
            total_lands += len(island)
            split = 2
            if len(island) == 2:
                continue
            for r, c in island:
                adjs = list(adj(r, c))
                if len(adjs) <= 1:
                    split = 1
                elif (
                    grid[r - 1][c] == grid[r + 1][c] == 0
                    or grid[r][c  - 1] == grid[r][c + 1] == 0
                    or grid[r - 1][c - 1] == grid[r + 1][c + 1] == 0
                    or grid[r - 1][c + 1] == grid[r + 1][c - 1] == 0
                ):
                    grid[r][c] = 0
                    after_split = bfs(*adjs[0])
                    if len(after_split) < len(island) - 1:
                        split = 1
                    # print(r, c, len(after_split))
                    grid[r][c] = 1
                if split == 1:
                    break
            # print(split)
            max_keep = max(max_keep, len(island) - split)
        
        return total_lands - max_keep
136