Under construction

Still being built. Things may change

Frogs in a Well

Time limit8sMemory limit1024 MB

Summary
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.
Level

Medium7 of 10

Topics
Sorting, BFS, Matrix
Solved
No attempts yet

Problem

There is a grid of evenly spaced lattice points with h×wh \times w points. The point in row rr from the top and column cc from the left is denoted (r,c)(r, c) (1≤r≤h1 \le r \le h, 1≤c≤w1 \le c \le w). Each lattice point holds one frog. Each frog has a strength value, and the frog at (r,c)(r, c) has strength fr,cf_{r,c}.

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 (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) is ∣r1−r2∣+∣c1−c2∣|r_1 - r_2| + |c_1 - c_2|. A frog at (r,c)(r, c) can be active within distance dd if no frog within distance dd is strictly stronger than it. That is, if fi,j≤fr,cf_{i,j} \le f_{r,c} for every 1≤i≤h1 \le i \le h and 1≤j≤w1 \le j \le w with ∣r−i∣+∣c−j∣≤d|r - i| + |c - j| \le d, then the frog at (r,c)(r, c) can be active within distance dd.

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 d=1,2,…,h+w−2d = 1, 2, \ldots, h + w - 2, the program counts the frogs that can be active within distance dd. 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 hh and ww with 1≤h≤1,0001 \le h \le 1{,}000, 1≤w≤1,0001 \le w \le 1{,}000, and h+w≥3h + w \ge 3. The next hh lines each contain ww integers, the strengths of the frogs. The cc-th integer on the rr-th line is fr,cf_{r,c}, with 1≤fr,c≤1091 \le f_{r,c} \le 10^9. The input ends with a line containing two zeros. There are at most 50 datasets.

Output

For each dataset, print h+w−2h + w - 2 integers separated by spaces on one line. The ii-th integer is the number of frogs for which no frog within distance ii is stronger than them.

Examples1

  1. Example 1

    Input
    4 5
    9 1 1 1 3
    5 2 1 3 1
    6 4 1 1 2
    4 4 3 2 8
    7 5
    24 18 10 34 43
    37 26 42 49 27
    10 41 3 19 29
    31 4 10 32 42
    44 25 41 14 28
    36 21 25 30 40
    1 43 8 40 44
    3 3
    1 1 1
    1 1 1
    1 1 1
    1 9
    1 2 3 4 5 4 3 2 1
    0 0
    
    Expected output
    7 4 2 2 2 2 1
    9 5 3 3 3 1 1 1 1 1
    9 9 9 9
    1 1 1 1 1 1 1 1