In the binary grid, 1 represents land and 0 represents water. An island is a group of land cells connected through shared edges. The grid is connected only when it has exactly one island; both zero islands and multiple islands count as disconnected. Each day, you may change one land cell into water. Return the fewest days needed to make the grid disconnected.
Example 1:
Input: grid = [[1,0,1]]
Output: 0Explanation: The two land cells are already separate islands. The grid is disconnected before any changes, so the answer is 0.
Example 2:
Input: grid = [[1,1,1]]
Output: 1Explanation: Change the middle cell to water. The remaining two land cells form separate islands, so 1 day is enough.
Example 3:
Input: grid = [[1,1],[1,1]]
Output: 2Explanation: Removing any one cell leaves a connected three-cell island. Removing two diagonally opposite cells leaves two separate islands, so the minimum is 2 days.
Constraints:
1 <= grid.length <= 30- Every row has the same length, between
1and30. - Each entry is either
0(water) or1(land).