#t148. 建造停机坪

建造停机坪

Building a Helipad

Problem Description

Xiao A has a terrain map with MM rows and NN columns, where the number aija_{ij} at row ii, column jj represents the altitude at coordinates (i,j)(i, j).

A helipad is a 3×33 \times 3 region whose 99 points have a maximum-to-minimum altitude difference of at most HH.

Xiao A asks you to compute, among all regions suitable for building a helipad, the maximum possible sum of the altitudes of the 99 points inside the region.

Input Format

The first line contains three positive integers M,N,HM, N, H, as described.

The following MM lines: the ii-th line contains NN integers ai1,ai2,…,aiNa_{i1}, a_{i2}, \dots, a_{iN}, the altitudes at coordinates (i,j)(i, j).

It is guaranteed that at least one region suitable for building a helipad exists.

Output Format

Output one line containing the maximum sum of altitudes.

Sample Input

5 5 3
5 5 5 5 5
5 1 5 1 5
5 5 5 5 5
5 2 5 2 5
3 5 5 5 2

Sample Output

40

Sample Explanation

Among all 3×33 \times 3 regions, the one spanning rows 33 to 55 and columns 11 to 33 satisfies the condition. Its 99 altitudes are:

  • Row 3: 5,5,55, 5, 5;
  • Row 4: 5,2,55, 2, 5;
  • Row 5: 3,5,53, 5, 5.

Their sum is 5+5+5+5+2+5+3+5+5=405 + 5 + 5 + 5 + 2 + 5 + 3 + 5 + 5 = 40. The maximum altitude in the region is 55 and the minimum is 22, so the altitude difference is 33, which does not exceed H=3H = 3. It can be verified that this is the largest altitude sum among all valid regions, so the output is 4040.

Constraints

For all test cases, 1≤M,N≤1031 \le M, N \le 10^3 and 1≤H,aij≤1051 \le H, a_{ij} \le 10^5 are guaranteed.