This page is still under construction.

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

2D Max Filter

Interview

Time limit1sMemory limit128 MB

Summary
For every cell of an R by C grid, output the maximum value inside the rectangular window centered on that cell, clipped at the borders.
Level

Medium5 of 10

Topics
Sliding window, Queue, Matrix
Solved
No attempts yet

Problem

An electrical engineer wants an efficient algorithm for filtering a 2D signal. The signal is digitized into blocks arranged in RR rows and CC columns, and every block holds one integer value.

The filter has a rectangular window covering (2M+1)(2M+1) rows and (2N+1)(2N+1) columns, where MM and NN are non-negative integers. The window moves across the entire input signal and produces another 2D signal under the following rules.

  1. When the window center is at row AA, column BB of the input signal, the window produces one value at row AA, column BB of the output signal.
  2. The produced value is the maximum input value inside the region the window covers.
  3. The part of the window that lies outside the input signal is not considered. Only the part inside the input signal is taken into account.

Rows and columns are numbered from 1. For example, when M=1M = 1 and N=2N = 2, the window is 3 rows by 5 columns. If its center is at row 4, column 5, the window covers the input region from row 3, column 3 to row 5, column 7, and the maximum value in that region becomes the output at row 4, column 5. If its center is at row 1, column 2, the window reaches from row -1, column -1 to row 2, column 4, but everything outside the signal is ignored, so the values actually taken into account are those from row 1, column 1 to row 2, column 4.

Given the input signal, write a program that computes the output signal of this 2D max filter.

Input

The first line contains RR and CC, the numbers of rows and columns of the input signal, separated by a space. (1≤R≤10001 \le R \le 1000, 1≤C≤10001 \le C \le 1000)

The second line contains MM and NN, separated by a space. (0≤M≤600 \le M \le 60, 0≤N≤600 \le N \le 60) The window size follows from MM and NN: it is (2M+1)(2M+1) rows by (2N+1)(2N+1) columns.

Each of the next RR lines holds one row of the input signal, given as CC integers from left to right, separated by spaces. Each value is an integer between 0 and 10000.

Output

Print the output signal on RR lines. Each line contains the CC output values of that row, separated by spaces.

Examples1

  1. Example 1

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