Under construction

Still being built. Things may change

Salt and Pepper (Small)

Time limit1sMemory limit512 MB

Summary
Given an M by N brightness matrix and odd window size W, output the median of every W by W window.
Level

Easy3 of 10

Topics
Array, Sorting, Brute force
Solved
No attempts yet

Problem

Gap and Eul are a couple. One evening they went to a restaurant for cream cheese spaghetti. The plate looked good, so Gap took a photo with a phone, and while getting ready to post it on social media Gap noticed the salt and pepper scattered over the spaghetti.

Gap did not like the salt and pepper in the photo and asked Eul to remove them. Eul answered that this was easy, pulled paper and a pen out of a bag, and wrote the problem down like this.

  • An M×NM \times N matrix AA describing the photo is given as input. Each entry is the brightness of one pixel, where 0 is the darkest and KK is the brightest.
  • An integer WW is also given as input. It defines a matrix BB as follows.
    • B[i][j]=median(A[i+x][j+y])B[i][j] = \mathrm{median}(A[i+x][j+y])
    • where 1≤i≤M−W+11 \le i \le M-W+1, 1≤j≤N−W+11 \le j \le N-W+1, 0≤x,y<W0 \le x, y < W.
    • A[i+x][j+y]A[i+x][j+y] is the entry in row i+xi+x and column j+yj+y of matrix AA, and median is the middle value.
  • Compute matrix BB from matrix AA and the integer WW.

How matrix BB is built from matrix AA and WW

Eul opened a laptop and started writing the program, but it did not go well and the spaghetti began to go soft. Write the program for Eul before the spaghetti is ruined.

The median of a set of values is the value that sits in the middle once they are sorted by size. For 1, 2, 3, 3, 100 the median is 3, because 3 is in the middle. WW is odd, so the number of values in one window, W2W^2, is odd as well and the median is always a single value.

Input

The first line contains the matrix size MM and NN (1≤M,N≤301 \le M, N \le 30), the maximum brightness KK (1≤K≤10 0001 \le K \le 10\,000), and the integer WW (1≤W≤min⁡(M,N)1 \le W \le \min(M, N)), separated by spaces. WW is odd.

Each of the next MM lines contains NN integers between 0 and KK. The jjth integer on the iith of these lines is the entry in row ii and column jj of matrix AA.

Output

Print matrix BB on M−W+1M-W+1 lines. Each line holds N−W+1N-W+1 integers separated by a single space.

Examples3

  1. Example 1

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

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

    Input
    5 5 20 3
    5 1 2 8 10
    12 10 3 20 7
    8 12 19 18 15
    17 19 2 5 13
    11 2 4 14 16
    
    Expected output
    8 10 10
    12 12 13
    11 12 14