← All problems

Number of Islands

Medium graphsdfsbfsmatrix LeetCode ↗
Count the islands (connected '1's, 4-directional) in a binary grid.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(mn), Space O(mn) worst-case recursion

Common pitfalls

  • 8-directional by mistake
  • Not marking visited (infinite loop)

Your solution

Log in to save your code and track progress.