Frogs in a Well
Time limit8sMemory limit1024 MB
For each distance d from 1 to h+w-2, count the frogs with no strictly stronger frog within Manhattan distance d on an h by w grid.
Problem
There is a grid of evenly spaced lattice points with points. The point in row from the top and column from the left is denoted (, ). Each lattice point holds one frog. Each frog has a strength value, and the frog at has strength .
However, every frog is a frog in a well: it believes it is the strongest in the world. Each frog intuitively knows the area where no stronger frog exists, and it keeps its identity by staying active only within that area.
The distance between and is . A frog at can be active within distance if no frog within distance is strictly stronger than it. That is, if for every and with , then the frog at can be active within distance .
To measure how much of a frog in a well each frog is, called the well degree, you are asked to write a program. For each distance , the program counts the frogs that can be active within distance . Write it and show that you are not a frog in the programming world.
Input
The input consists of multiple datasets. Each dataset has the following format.
h w
f1,1 f1,2 ... f1,w
f2,1 f2,2 ... f2,w
...
fh,1 fh,2 ... fh,w
The first line contains two integers and with , , and . The next lines each contain integers, the strengths of the frogs. The -th integer on the -th line is , with . The input ends with a line containing two zeros. There are at most 50 datasets.
Output
For each dataset, print integers separated by spaces on one line. The -th integer is the number of frogs for which no frog within distance is stronger than them.