238. Number of Islands

Medium · Depth-First Search

Given an m×n 2D binary grid where "1" is land and "0" is water, count the number of islands. An island is surrounded by water and formed by connecting adjacent lands horizontally or vertically.

Examples

Example 1
Input: grid=[["1","1","0"],["1","1","0"],["0","0","1"]]
Output: 2
Explanation: Two disconnected land masses

Constraints