Description

Medium

The Coral Reef Ecosystem

Marine biologists are studying coral reef formations on the ocean floor. The ocean floor is represented as an m x n grid where:

  • 1 represents coral
  • 0 represents water

A coral reef ecosystem is a group of coral cells (1s) connected horizontally or vertically (not diagonally). Each cell can only belong to one ecosystem.

Count the total number of distinct coral reef ecosystems in the grid.

Input Format

The first line contains two integers m and n (rows and columns). The next m lines each contain n space-separated integers (0 or 1).

Output Format

Print a single integer representing the number of distinct coral reef ecosystems.

Examples

Example 1

Input
4 5 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1
Output
3
ExplanationThere are 3 distinct ecosystems: top-left 2x2 coral, middle single coral, bottom-right 1x2 coral.

Example 2

Input
3 3 1 0 1 0 0 0 1 0 1
Output
4
ExplanationEach corner coral is isolated, forming 4 separate ecosystems.

Constraints

1 ≤ m, n ≤ 100

grid[i][j] is either 0 or 1

Code

Login
Error

Case 1

Case 2

Input
4 5 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1
Output
No output
Expected
3