#t149. 建造停机坪

建造停机坪

Building a Helipad

Problem Description

Little A has a terrain map of MM rows and NN columns, where the number aija_{ij} at row ii, column jj is the altitude at coordinate (i,j)(i, j).

A helipad is a 3×33 \times 3 region whose 99 cells have a difference between the maximum and minimum altitude of at most HH.

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

Input Format

The first line contains three positive integers MM, NN, and HH, 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 each coordinate. It is guaranteed that at least one helipad-suitable region exists.

Output Format

Output one line: the maximum altitude sum.

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 from row 33 column 11 to row 55 column 33 satisfies the condition. Its 99 altitudes are:

row 33: 5,5,55, 5, 5; row 44: 5,2,55, 2, 5; row 55: 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, the minimum is 22, the 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 answer is 4040.

Constraints

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