Landscape Painting
Time limit1sMemory limit1024 MB
Given an N by M grid of black and white cells, count the mountains and lakes of each size from 1 to N.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
Yun's hobby is drawing. One day, inspired by the hill behind the school, Yun painted a landscape. The landscape is a grid of rows and columns, and each cell is painted black or white.
Yun decided to give the painting to Phoenix and Dalgu. However, there was only one painting, so Phoenix and Dalgu began arguing over who should get it. The debate did not end easily because the two had different standards for a good landscape.
Dalgu thinks a landscape with many mountains is good, and Phoenix thinks a landscape with many lakes is good. The definitions of a mountain and a lake are as follows. denotes the cell in row and column .
- If every satisfying is black, this is called a mountain of size centered at . Here must be an integer with .
- If every satisfying is white, this is called a lake of size with its top-left corner at . Here must be an integer with .

In the figure above, there is a mountain of size centered at and a lake of size with its top-left corner at .
To stop the debate, Yun wants to count, for every from to , the number of mountains of size and the number of lakes of size . Help Yun solve this problem.
Input
The first line contains the number of rows and the number of columns , separated by a space.
From the second line onward, the next lines describe the landscape. Line is a string of length representing row of the landscape. Every string consists of # or .. # means black, and . means white.
Output
On the first line, print integers , separated by spaces, where is the number of mountains of size .
On the second line, print integers , separated by spaces, where is the number of lakes of size .