You're given a 2D grid of 1s (land) and 0s (water). An island is a group of 1s connected horizontally or vertically. Return the number of islands.
This is exactly the grid-traversal idea behind BFS and DFS — instead of finding a path, you're flood-filling each connected region.
Example
Input: grid = [
[1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
]
Output: 3