This page is still under construction.

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

Taeyoung Throws Bombs

Interview

Time limit2sMemory limit512 MB

Summary
Given the altitude grid after all first explosions, recover how many bombs landed at each cell, where each bomb lowers an MxM square by 1.
Level

Medium6 of 10

Topics
Prefix sum, Implementation, Array, Simulation
Solved
No attempts yet

Problem

Taeyoung, having failed his exam, throws bombs at Inha University!

Inha University is a square plot of land of size N×N. All of the land is divided into 1×1 square cells. Each cell is denoted by (r, c), where r is the number of cells from the top and c is the number of cells from the left. r and c start at 0.

Initially, every cell of Inha University has an altitude of 0 meters. However, when Taeyoung throws a bomb, the altitude of every cell within the blast range decreases by 1 meter, and the bomb remains where it landed. The blast range of a bomb Taeyoung throws is a square region of size M×M centered on the cell where the bomb landed. M is odd, and the top edge of the blast range is parallel to the top edge of Inha University. Taeyoung never throws a bomb so that its blast range goes beyond Inha University.

The bombs Taeyoung throws are designed to explode not just once but once more 3 days later. You are given the altitude of Inha University after Taeyoung threw the bombs and all of them finished their first explosion. The altitude of each cell is H[r][c]. Our task is to find, for every cell, how many bombs are there, before the bombs explode once more 3 days later.

Input

The positive integers N and M are given, separated by a space.

N lines follow, giving the values of the array H. The c-th value on the r-th line is H[r][c].

Output

Output N lines, each containing N integers.

The c-th value on the r-th line is the number of bombs at (r, c).

Constraints

  • 1 ≤ M ≤ N ≤ 2,000 (M is odd)
  • -2,147,483,648 ≤ H[r][c] ≤ 0

Examples3

  1. Example 1

    Input
    5 3
    -2 -2 -4 -2 -2
    -2 -2 -4 -2 -2
    -2 -2 -4 -2 -2
    0 0 0 0 0
    0 0 0 0 0
    
    Expected output
    0 0 0 0 0
    0 2 0 2 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    
  2. Example 2

    Input
    5 3
    -8 -17 -26 -18 -9
    -9 -29 -47 -38 -18
    -10 -38 -62 -52 -24
    -2 -21 -36 -34 -15
    -1 -9 -15 -14 -6
    
    Expected output
    0 0 0 0 0
    0 8 9 9 0
    0 1 11 9 0
    0 1 8 6 0
    0 0 0 0 0
    
  3. Example 3

    Input
    10 3
    -8 -17 -26 -19 -21 -21 -21 -18 -9 -8
    -14 -36 -56 -43 -40 -39 -42 -36 -17 -13
    -20 -50 -70 -53 -46 -54 -60 -59 -31 -22
    -22 -48 -70 -55 -48 -48 -53 -48 -26 -14
    -18 -33 -52 -50 -60 -66 -68 -64 -39 -21
    -17 -31 -63 -62 -69 -53 -51 -45 -29 -15
    -13 -31 -59 -71 -75 -66 -53 -54 -35 -23
    -12 -37 -60 -65 -53 -49 -38 -43 -24 -18
    -7 -25 -35 -43 -38 -47 -37 -39 -20 -15
    -1 -10 -13 -13 -9 -19 -21 -23 -10 -7
    
    Expected output
    0 0 0 0 0 0 0 0 0 0
    0 8 9 9 1 11 9 1 8 0
    0 6 13 11 0 8 10 3 5 0
    0 6 8 0 2 4 9 5 9 0
    0 10 5 11 5 7 3 4 0 0
    0 2 2 8 9 14 13 9 12 0
    0 5 7 13 2 0 0 1 3 0
    0 6 9 7 14 8 6 2 8 0
    0 1 9 3 1 5 13 3 7 0
    0 0 0 0 0 0 0 0 0 0