147: Number of islands
Given a 2D matrix, the matrix contains 0 (water) and 1(land), return number of islands
Note: An
islandis surrounded by water and is formed byconnecting adjacent landshorizontally or vertically. All four edges of the matrix are all surrounded by water
Example 1
Input: 0 0 0 0 0
0 1 1 0 0
0 1 1 0 1
0 0 0 1 1
Output: 2
Example 2
Input: 1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
Output: 4