Under construction

Still being built. Things may change

Density Map

Time limit3sMemory limit128 MB

Summary
For each cell of an n by n binary grid, sum the entries within Chebyshev distance r, using a 2D sliding-window or prefix-sum over the square neighborhood.
Level

Medium4 of 10

Topics
Prefix sum, Matrix, Sliding window
Solved
No attempts yet

Problem

You are given integers nn and rr with n>r≥0n > r \ge 0, together with an n×nn \times n table FF whose entries belong to {0,1}\{0, 1\}. The columns and rows of the table are numbered from 11 to nn, and the value in column ii, row jj is denoted F[i,j]F[i, j].

The distance between two positions [i,j][i, j] and [i′,j′][i', j'] is defined as max⁡(∣i−i′∣, ∣j−j′∣)\max(|i - i'|,\ |j - j'|).

Compute the n×nn \times n table WW, where W[i,j]W[i, j] equals the sum of all entries F[x,y]F[x, y] such that the distance between [x,y][x, y] and [i,j][i, j] is at most rr.

Write a program that reads the integers nn and rr and the table FF from standard input, computes the table WW, and writes it to standard output.

Input

The first line contains two integers nn and rr, separated by a single space, with 0≤r<n≤2500 \le r < n \le 250. Each of the next nn lines describes the table FF and contains nn integers from {0,1}\{0, 1\} separated by single spaces. The ii-th number on the (j+1)(j+1)-st line equals F[i,j]F[i, j].

Output

Print exactly nn lines. On the jj-th line, print the values W[1,j],W[2,j],…,W[n,j]W[1, j], W[2, j], \dots, W[n, j] in order, separated by single spaces.

Examples3

  1. Example 1

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

    Input
    4 0
    1 0 1 1
    0 0 1 0
    1 1 0 0
    0 1 1 1
    
    Expected output
    1 0 1 1
    0 0 1 0
    1 1 0 0
    0 1 1 1
    
  3. Example 3

    Input
    3 2
    1 0 1
    1 1 0
    0 0 1
    
    Expected output
    5 5 5
    5 5 5
    5 5 5