#K68612. Count Islands
Count Islands
Count Islands
Given a grid of size containing binary values (0 and 1), where 1 represents land and 0 represents water, count the number of islands. An island is formed by connecting adjacent land cells horizontally or vertically.
Input: The first line contains integers and . The next lines each contain integers representing the grid.
Output: Print a single integer representing the number of islands present in the grid.
inputFormat
Input is read from standard input (stdin). The first line contains two integers, and , representing the number of rows and columns, respectively. Then follow lines, each containing space-separated integers (0 or 1) representing the grid.
outputFormat
Output a single integer representing the number of islands present in the grid. The result is printed to standard output (stdout).## sample
4 5
1 1 0 0 0
0 1 0 0 1
1 0 0 1 1
0 0 0 0 0
3