Submit solution

Points: 8 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type

Kian is an urban planner for the city of Marlow. The city is preparing a flood-resilience plan, and Kian has been given a satellite survey of a district. The survey is a grid with N rows and M columns. Each cell is either a developed lot or open ground.
A neighborhood is a group of developed lots that are connected horizontally or vertically (not diagonally). Each neighborhood will get its own evacuation plan, so Kian can’t afford to miscount. Everything outside the grid is considered open ground.

Input Specification

The first two lines contain N and M (1 \le N, M \le 300), representing the number of rows and columns in the survey respectively.

Each of the next N lines contains a string of exactly M characters. The j-th character of the i-th line is 1 if the cell at row i, column j is a developed lot, and 0 if it is open ground.

Output Specification

Output a single integer, the number of neighborhoods in the district.

Sample Input 1

4 
5  
11110  
11010  
11000  
00000

Sample Output 1

1

Sample Input 2

9 
8  
11000001  
11000011  
00100110  
00011100
00010000
00011100
00110000
00011111
00001000

Sample Output 2

3


Note: this problem was designed and tested with DFS in mind, but BFS should work as well.


Comments

There are no comments at the moment.