Number of Islands

00:00
MediumGraphBFSDFSMatrixUnion Find
AmazonGoogleFacebook

Count the number of islands in an m×n grid of '1's (land) and '0's (water). Islands are formed by connecting adjacent land horizontally/vertically.

Examples

Input → [[1,1,1],[0,1,0],[1,1,1]]
Output → 1
Input → [[1,1,0],[0,0,0],[0,0,1]]
Output → 2
Input → [[1,0],[0,1]]
Output → 2