Under construction

Still being built. Things may change

K-Triangles

Time limit3sMemory limit512 MB

Summary
Given an n by m integer matrix and k, find the maximum total sum of two non-overlapping k-triangles.
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum, Matrix
Solved
No attempts yet

Problem

You are given an n×mn \times m matrix AA of integers and a positive integer kk.

For a cell (x0,y0)(x_0, y_0) of AA, the kk-triangles centered at (x0,y0)(x_0, y_0) are defined as follows.

  • If 1≤x0≤n−k+11 \le x_0 \le n-k+1 and 1≤y0≤m−k+11 \le y_0 \le m-k+1, then T0={(x,y):x≥x0,y≥y0,∣x−x0∣+∣y−y0∣<k}T_0 = \{(x, y) : x \ge x_0, y \ge y_0, |x-x_0| + |y-y_0| < k\}.
  • If k≤x0≤nk \le x_0 \le n and 1≤y0≤m−k+11 \le y_0 \le m-k+1, then T1={(x,y):x≤x0,y≥y0,∣x−x0∣+∣y−y0∣<k}T_1 = \{(x, y) : x \le x_0, y \ge y_0, |x-x_0| + |y-y_0| < k\}.
  • If k≤x0≤nk \le x_0 \le n and k≤y0≤mk \le y_0 \le m, then T2={(x,y):x≤x0,y≤y0,∣x−x0∣+∣y−y0∣<k}T_2 = \{(x, y) : x \le x_0, y \le y_0, |x-x_0| + |y-y_0| < k\}.
  • If 1≤x0≤n−k+11 \le x_0 \le n-k+1 and k≤y0≤mk \le y_0 \le m, then T3={(x,y):x≥x0,y≤y0,∣x−x0∣+∣y−y0∣<k}T_3 = \{(x, y) : x \ge x_0, y \le y_0, |x-x_0| + |y-y_0| < k\}.

The cost of a kk-triangle TT is f(T)=∑(x,y)∈TAx,yf(T) = \sum_{(x,y) \in T} A_{x,y}.

Find the maximum possible total cost of two non-intersecting kk-triangles. Formally, find max⁡P∪Q=∅f(P)+f(Q)\max_{P \cup Q = \emptyset}{f(P) + f(Q)}, where PP and QQ are some kk-triangles.

Input

The first line contains three space-separated integers nn, mm, and kk (1≤n,m,k≤15001 \le n, m, k \le 1500). Each of the next nn lines contains mm space-separated integers. The ii-th line contains Ai,1,Ai,2,…,Ai,mA_{i,1}, A_{i,2}, \dots, A_{i,m} (−109≤Ai,j≤109-10^9 \le A_{i,j} \le 10^9). It is guaranteed that there is at least one way to choose two non-intersecting kk-triangles in the matrix.

Output

Print a single integer: the maximum possible sum of costs of two non-intersecting kk-triangles.

Hint

In the second example, the answer is made up of two kk-triangles of type T1T_1 with centers at (4,1)(4, 1) and (3,3)(3, 3).

Examples2

  1. Example 1

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

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