Under construction

Still being built. Things may change

Digging for Oil

Time limit2sMemory limit128 MB

Summary
Place three non-overlapping K by K squares on an M by N grid of oil estimates to maximize the total sum covered, with the grid up to 1500 by 1500.
Level

Hard8 of 10

Topics
Prefix sum, Dynamic programming, Array, Implementation
Solved
No attempts yet

Problem

The government of Siruseri has decided to auction off, to oil-development contractors, the land of Navaru province where crude oil is abundant. The whole area up for auction is divided into small blocks that form an M×NM \times N rectangular grid.

Siruseri's geological survey bureau has an estimate of the crude-oil content of each block; these estimates are given as a non-negative integer in each cell of the M×NM \times N grid.

To prevent monopolies, the government requires that each contractor may bid on exactly one contiguous K×KK \times K square of blocks.

The AoE Oil Association, made up of three contractors, wants to secure as much oil as possible by choosing three non-overlapping K×KK \times K square regions.

For example, for the grid in the sample input below, the best achievable total is 100100 when K=2K = 2 and 208208 when K=3K = 3.

Write a program that finds the maximum total estimated oil content the AoE Association can secure.

Input

The first line contains three integers MM, NN, and KK, where MM and NN are the numbers of rows and columns of the grid, and KK is the side length of the square region a contractor bids on. Each of the next MM lines contains the oil-content estimates of the NN blocks in one row, given as non-negative integers.

K≤MK \le M and K≤NK \le N, and at least three non-overlapping K×KK \times K regions exist. Also M,N≤1500M, N \le 1500, and every block's estimate is a non-negative integer less than 500500.

Output

Print, on a single line, the maximum total estimated oil content the AoE Oil Association can secure.

Examples4

  1. Example 1

    Input
    9 9 3
    1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1
    1 8 8 8 8 8 1 1 1
    1 8 8 8 8 8 1 1 1
    1 8 8 8 8 8 1 1 1
    1 1 1 1 8 8 8 1 1
    1 1 1 1 1 1 8 8 8
    1 1 1 1 1 1 9 9 9
    1 1 1 1 1 1 9 9 9
    
    Expected output
    208
    
  2. Example 2

    Input
    9 9 2
    1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1
    1 8 8 8 8 8 1 1 1
    1 8 8 8 8 8 1 1 1
    1 8 8 8 8 8 1 1 1
    1 1 1 1 8 8 8 1 1
    1 1 1 1 1 1 8 8 8
    1 1 1 1 1 1 9 9 9
    1 1 1 1 1 1 9 9 9
    
    Expected output
    100
    
  3. Example 3

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

    Input
    3 9 3
    9 9 9 1 1 1 5 5 5
    9 9 9 1 1 1 5 5 5
    9 9 9 1 1 1 5 5 5
    
    Expected output
    135