Maximum Sum
InterviewTime limit2sMemory limit1024 MB
Given an m by n grid of integers, find the axis-aligned rectangle with the largest sum along its border and print that sum plus the corner coordinates.
- Level
Medium6 of 10
- Topics
- Prefix sum, Brute force, Implementation, Array
- Solved
- No attempts yet
Problem
Today the newspaper <> published an unusual mathematical puzzle. One page of the newspaper is completely filled with a rectangular table with rows and columns. Each cell of the table contains an integer.
To solve the puzzle, you must find a nondegenerate rectangle whose vertices lie at the centers of table cells and whose sides are parallel to the sides of the table, such that the sum of the numbers written in the cells on the border of the rectangle is as large as possible.

After spending several hours on the puzzle without success, Sasha decided to write a program that would solve it for him. But here too he failed. Now he has no choice but to ask you for help.
Write a program that, given a table, finds the required rectangle.
Input
The first line of the input file contains two integers and (). Then follows a description of the table: lines, each containing integers ().
Output
On the first line of the output file, print the integer , the maximum sum of the numbers on the border of the required rectangle. On the second line, print four positive integers: , the coordinates of the top-left and bottom-right cells of the chosen rectangle, respectively. Here is the row number and is the column number; rows are numbered from top to bottom starting from one, and columns are numbered from left to right starting from one. If there are several optimal solutions, print any of them.