Under construction

Still being built. Things may change

Calligrapher

Time limit1sMemory limit512 MB

Summary
Choose disjoint placements of the letters N, O, and I on an n by m grid of signed values to maximize the total sum of covered cells.
Level

Hard9 of 10

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

Problem

Little E really likes calligraphy. He heard that NOI2013 has started, and he wants to give a calligraphic design of "NOI" to everyone.

Little E has one sheet of magical paper. The paper is a two-dimensional grid with nn rows and mm columns. The bottom-left corner has coordinates (1,1)(1, 1) and the top-right corner has coordinates (m,n)(m, n). Each cell contains an integer luckiness value. Writing on a cell increases everyone's luckiness. The overall luckiness is the sum of the luckiness values of all cells that have been written on. You need to write the three letters N, O, and I onto the paper.

The three letters are defined as follows.

  1. N is made up of some number (≥3\geq 3) of rectangles parallel to the grid's axes. Let KK be the number of rectangles, numbered from 1 to KK. The ii-th rectangle has bottom-left corner (Li,Bi)(L_i, B_i) and top-right corner (Ri,Ti)(R_i, T_i). These values satisfy:

    1. Li≤RiL_i \leq R_i, Bi≤TiB_i \leq T_i;
    2. For all 1<i≤K1 < i \leq K, Li=Ri−1+1L_i = R_{i-1} + 1;
    3. For all 3≤i<K3 \leq i < K, Bi−1−1≤Ti≤Ti−1B_{i-1} - 1 \leq T_i \leq T_{i-1}, Bi≤Bi−1B_i \leq B_{i-1};
    4. B2>B1B_2 > B_1, T2=T1T_2 = T_1, BK−1=BKB_{K-1} = B_K, TK−1<TKT_{K-1} < T_K.
  2. O is a bigger rectangle AA with a smaller rectangle BB carved out of it. Both have sides parallel to the grid's axes. Let (u,v)(u, v) be the bottom-left corner of AA, and let WW and HH be its width and height. Then BB has bottom-left corner (u+1,v+1)(u+1, v+1), width W−2W-2, and height H−2H-2. These values satisfy:

    1. W≥3W \geq 3, H≥3H \geq 3;
    2. u>RK+1u > R_K + 1.
  3. I is made up of 3 rectangles with sides parallel to the grid's axes, numbered 1, 2, and 3 from bottom to top. Let (Pi,Qi)(P_i, Q_i) be the bottom-left corner of rectangle ii, and (Gi,Hi)(G_i, H_i) its top-right corner. These values satisfy:

    1. Pi≤GiP_i \leq G_i, Qi≤HiQ_i \leq H_i;
    2. P1=P3>u+WP_1 = P_3 > u + W, G1=G3G_1 = G_3;
    3. Q1=H1=Q2−1Q_1 = H_1 = Q_2 - 1, H2+1=Q3=H3H_2 + 1 = Q_3 = H_3;
    4. P1<P2≤G2<G1P_1 < P_2 \leq G_2 < G_1.

The following figure shows a valid design of N, O, and I.

All shapes in the design must stay within the boundaries of the grid. Little E wants to find the maximum possible luckiness that his design can produce.

Input

The first line contains two positive integers nn and mm, the number of rows and columns. Each of the next nn lines contains mm integers. The jj-th integer on line i+1i + 1 is the luckiness of the cell at (j,n−i+1)(j, n - i + 1).

Output

Output a single integer TT, the maximum total luckiness that the design can produce.

Constraints

3≤n≤1503 \leq n \leq 150

3≤m≤5003 \leq m \leq 500

Each luckiness value is an integer in [−200,200][-200, 200].

Examples2

  1. Example 1

    Input
    3 13
    1 1 -1 -1 1 -1 1 1 1 -1 1 1 1
    1 -1 1 -1 1 -1 1 -1 1 -1 -1 1 -1
    1 -1 -1 1 1 -1 1 1 1 -1 1 1 1
    
    Expected output
    24
    
  2. Example 2

    Input
    3 13
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
    
    Expected output
    -20