Minimum Number of Days to Disconnect Island

Given a 2D grid consisting of 1s (land) and 0s (water). An island is a maximal 4-directionally (horizontal or vertical) connected group of 1s.

The grid is said to be connected if we have exactly one island, otherwise is said disconnected.

In one day, we are allowed to change any single land cell (1) into a water cell (0).

Return the minimum number of days to disconnect the grid.

Example 1:

Example 2:

Example 3:

Example 4:

Example 5:

Constraints:

  • 1 <= grid.length, grid[i].length <= 30

  • grid[i][j] is 0 or 1.

Last updated