PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000224. Tổng vùng lân cận của ma trận (Matrix Block Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng vùng lân cận của ma trận (Matrix Block Sum)

Matrix Block Sum

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given an m×nm\times n matrix aa and an integer KK, construct a matrix bb of the same size. Entry bi,jb_{i,j} is the sum of original cells (r,c)(r,c) within the matrix with ∣r−i∣≤K|r-i|\le K and ∣c−j∣≤K|c-j|\le K. Clip at the boundary; do not wrap or reuse updated entries.

Input

The first line contains m,n,Km,n,K. The next mm lines each contain nn integers.

Output

Print mm rows, each containing nn space-separated integers of the result matrix.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤81\le m,n\le 8; 1≤K≤1001\le K\le100; 1≤ai,j≤1001\le a_{i,j}\le100.
  • Subtask 2 (30 points): 1≤m,n≤1001\le m,n\le 100; 1≤K≤1001\le K\le100; 1≤ai,j≤1001\le a_{i,j}\le100; K≥max⁡(m,n)K\ge\max(m,n).
  • Subtask 3 (50 points): 1≤m,n≤1001\le m,n\le 100; 1≤K≤1001\le K\le100; 1≤ai,j≤1001\le a_{i,j}\le100.

Example 1

Input

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

Output

12 21 16
27 45 33
24 39 28

Explanation

The top-left cell sums four entries to 12; the center includes all entries.

Example 2

Input

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

Output

45 45 45
45 45 45
45 45 45

Explanation

Every neighborhood covers the full matrix, giving 45 everywhere.