Marlow
View as PDFKian 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 rows and
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 and
, representing the number of rows and columns in the survey respectively.
Each of the next lines contains a string of exactly
characters. The
-th character of the
-th line is
1 if the cell at row , column
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