#T370. 连通块

连通块

Description

An n * m grid has some squares colored black, marked as 1 in the grid, while white squares are marked as 0. Determine the number of four-connected black square regions. A four-connected black square region refers to an area composed of black squares where each black square can reach any other black square in the region by moving in four directions (up, down, left, right) while only traversing black squares.

Input Format

The first line contains two integers, n and m (1 ≤ n, m ≤ 100), representing the dimensions of the grid.
The next n lines each contain m integers, either 0 or 1, indicating whether the square is black or white.

Output Format

A single integer ans, representing the number of black square regions in the grid.

3 3
1 1 1
0 1 0
1 0 1


3