problems / 187

Suki's Garden Visibility

easy matrix simulation arrays 100 ms · 256 MB

Suki's Garden Visibility

Suki, a black curly-coated dog with a grey beard and white paws, is standing somewhere in her rectangular garden. The garden contains trees at various heights. From where she stands, Suki looks outward in each of the 8 compass directions (north, south, east, west, and the four diagonals) and counts how many trees she can see in each direction. A tree blocks her view of any tree directly behind it that is not strictly taller.

Input

The first line contains n and m (1 ≤ n, m ≤ 500), the dimensions of the garden grid.

The next n lines each contain m positive integers, the heights of the trees in the garden (1 ≤ height ≤ 10^9).

The next line contains two integers r and c (0 ≤ r < n, 0 ≤ c < m), Suki's position (0-indexed row and column).

Output

Print 8 integers on a single line, separated by spaces: the number of visible trees in each direction in this order: North, South, East, West, NorthEast, NorthWest, SouthEast, SouthWest.

Notes

A tree is visible from Suki's position in a direction if it is strictly taller than every tree between Suki and it in that direction. Shorter trees are hidden, but they don't end the ray: a taller tree further out is still visible.

Example 1

Input

3 3
1 2 3
4 5 6
7 8 9
1 1

Output

1 1 1 1 1 1 1 1

Example 2

Input

5 5
10 1 1 1 10
1 1 1 1 1
1 1 5 1 1
1 1 1 1 1
10 1 1 1 10
2 2

Output

1 1 1 1 2 2 2 2

Hints · reveal one at a time if you're stuck

Hint 1

Look at one direction at a time. A tree is counted only if it is strictly taller than every tree between it and Suki along that ray. What does that say about which trees along one ray get counted?

Hint 2

Walk outward along each ray once, keeping the tallest height seen so far and counting each tree that beats it. A tree that does not beat the running maximum does not change it.

Hint 3

Suki's own cell is not counted, and each ray stops when it reaches the edge of the garden, so diagonal rays can be shorter than the others.

report an issue Prefer your own editor? sooks prep 187 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.