Calligrapher
Time limit1sMemory limit512 MB
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 rows and columns. The bottom-left corner has coordinates and the top-right corner has coordinates . 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.
-
N is made up of some number () of rectangles parallel to the grid's axes. Let be the number of rectangles, numbered from 1 to . The -th rectangle has bottom-left corner and top-right corner . These values satisfy:
- , ;
- For all , ;
- For all , , ;
- , , , .
-
O is a bigger rectangle with a smaller rectangle carved out of it. Both have sides parallel to the grid's axes. Let be the bottom-left corner of , and let and be its width and height. Then has bottom-left corner , width , and height . These values satisfy:
- , ;
- .
-
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 be the bottom-left corner of rectangle , and its top-right corner. These values satisfy:
- , ;
- , ;
- , ;
- .
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 and , the number of rows and columns. Each of the next lines contains integers. The -th integer on line is the luckiness of the cell at .
Output
Output a single integer , the maximum total luckiness that the design can produce.
Constraints
Each luckiness value is an integer in .