This page is still under construction.

Parts of this page are still being built. What you see may change.

Sunsets

Time limit1sMemory limit128 MB

Summary
For each cell of an n x n grid, output the tallest height among all cells within Manhattan distance k.
Level

Medium7 of 10

Topics
Dynamic programming, Prefix sum, Matrix
Solved
No attempts yet

Problem

Residents of a small city love watching sunsets from the rooftops of their houses. When a sunset is especially spectacular, some of them climb onto the roofs of nearby buildings to get a better view.

The city's buildings sit on an n×nn \times n grid, and the distance between two buildings is measured with the Manhattan metric.

John is about to buy a new flat. He loves sunsets and is willing to walk to another building every evening for a better view, but he will not walk more than kk units away from his own flat.

For each building aa, determine the height of the tallest building John could walk to (every building within Manhattan distance kk, including building aa itself) if he bought his flat in building aa. Output the resulting map of these heights.

As a reminder, the Manhattan distance between two points (ax,ay)(a_x, a_y) and (bx,by)(b_x, b_y) is ρ((ax,ay),(bx,by))=∣ax−bx∣+∣ay−by∣\rho((a_x, a_y), (b_x, b_y)) = |a_x - b_x| + |a_y - b_y|.

Input

The first line contains two integers nn and kk (1≤n≤15001 \le n \le 1500, 1≤k≤n1 \le k \le n) separated by a single space. Each of the next nn lines contains nn non-negative integers, each at most 10910^9, separated by single spaces; these are the heights of the buildings, given row by row.

Output

Print nn lines, each containing nn non-negative integers separated by single spaces. The integer in row ii, column jj is the height of the tallest building within Manhattan distance kk of building (i,j)(i, j).

Examples2

  1. Example 1

    Input
    4 2
    1 3 4 5
    0 2 2 3
    4 1 1 3
    6 2 3 0
    
    Expected output
    4 5 5 5
    6 4 5 5
    6 6 4 5
    6 6 6 3
    
  2. Example 2

    Input
    3 1
    1 2 3
    4 5 6
    7 8 9
    
    Expected output
    4 5 6
    7 8 9
    8 9 9