Under construction

Still being built. Things may change

Image Correction Task

Time limit3sMemory limit1024 MB

Summary
Given a grid of sharpness values, correct at most K cells to the maximum X so the largest difference between adjacent cells is as small as possible.
Level

Hard8 of 10

Topics
Binary search, Graph
Solved
No attempts yet

Problem

Hyundai Mobis is a global auto parts company with strong research in smart mobility, including autonomous driving, connectivity, and electrification. The company develops software for autonomous driving and driver assistance, including software that processes images from vehicle cameras. Images can be unclear at night or in bad weather, so correcting images with software after capture matters.

An image from a vehicle camera can be split into NN rows and MM columns, giving NMNM regions in total. The clarity of the image in each region is an integer called sharpness.

Correcting one region sets its sharpness to the maximum sharpness XX. Ideally every region would be corrected, but the driver waits too long if correction takes time, so at most KK regions are corrected.

If two adjacent regions differ a lot in sharpness, the image looks unnatural to the driver. Two regions are adjacent if they share an edge. The largest sharpness difference between adjacent regions should be as small as possible.

Find the minimum possible value of the largest sharpness difference between adjacent regions after correcting at most KK regions.

Input

The first line contains NN, MM, KK, and XX, separated by spaces. Each of the next NN lines contains MM integers ai,1,ai,2,⋯ ,ai,Ma_{i,1}, a_{i,2}, \cdots, a_{i,M}, separated by spaces. ai,ja_{i,j} is the sharpness of the region in row ii from the top and column jj from the left.

Output

Print the minimum possible value of the largest sharpness difference between adjacent regions after correction.

Constraints

  • 1≤N,M≤5001 \le N, M \le 500
  • 0≤K≤NM0 \le K \le NM
  • 1≤X≤10181 \le X \le 10^{18}
  • 0≤ai,j≤X0 \le a_{i,j} \le X

Examples3

  1. Example 1

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

    Input
    1 3 1 10
    1 5 9
    
    Expected output
    4
    
  3. Example 3

    Input
    1 1 0 5
    1
    
    Expected output
    0