Loading...
You are given a 2D grid representing a satellite image. Each cell is either land (1) or water (0). The image may contain multiple islands, each formed by connected land cells.
Land connectivity uses 8-directional adjacency, including diagonals. Water connectivity uses 4-directional adjacency, no diagonals.
A lake is a contiguous region of water, connected via 4-directional adjacency, that is completely enclosed by land of a single island, meaning it does not touch any edge of the grid. Water that reaches the grid boundary is ocean, not a lake.
Given the grid and a starting coordinate guaranteed to be a land cell on one of the islands, return the number of lakes within that island.
Input:
image = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
start = [1, 1]
Output: 1
Explanation: One lake enclosed by the island.
Input:
image = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
]
start = [1, 1]
Output: 0
Explanation: Solid land block with no enclosed water.
rows, cols ≤1000rows × cols ≤106Click "Run" to test with sample cases or "Submit" to run all tests.