#K72207. Count Contiguous Hills

    ID: 33702 Type: Default 1000ms 256MiB

Count Contiguous Hills

Count Contiguous Hills

You are given a grid of dimensions \(N\) by \(M\) where each cell is either a hill (represented by '1') or a valley (represented by '0'). Two hills are considered to be in the same group (or contiguous) if they are adjacent horizontally or vertically, but not diagonally. Your task is to count the number of distinct groups of contiguous hills in the grid.

Input Format:

  • The first line of input contains two space-separated integers, \(N\) and \(M\), representing the number of rows and columns respectively.
  • The following \(N\) lines each contain a string of \(M\) characters ('0' or '1').

Output Format:

  • Output a single integer that represents the number of distinct contiguous groups of hills.

inputFormat

The input is given via stdin. The first line contains two integers \(N\) and \(M\). The next \(N\) lines each contain a string of length \(M\) representing the grid.

outputFormat

Output the number of contiguous groups of hills to stdout as an integer.

## sample
5 5
11100
11000
00100
00011
00011
3