#K95577. Maximum Sub-Rectangle Sum

    ID: 38894 Type: Default 1000ms 256MiB

Maximum Sub-Rectangle Sum

Maximum Sub-Rectangle Sum

Given an \(n \times m\) matrix of integers, find the sub-rectangle with the maximum sum of its elements. A sub-rectangle is defined as a contiguous block of cells within the matrix. Formally, if we denote the element in the \(i\)-th row and \(j\)-th column as \(a_{ij}\), then the sum of a sub-rectangle defined by the top left corner \((i, j)\) and the bottom right corner \((k, l)\) is given by:

\[ S = \sum_{p=i}^{k} \sum_{q=j}^{l} a_{pq} \]

Your task is to find the maximum value of \(S\) over all possible sub-rectangles in the given matrix. Note that the matrix may contain negative numbers.

inputFormat

The first line of input contains two integers \(n\) and \(m\) representing the number of rows and columns of the matrix, respectively. This is followed by \(n\) lines, each containing \(m\) space-separated integers representing the matrix elements.

Example:

3 3
1 2 -1
-3 4 2
1 -5 3

outputFormat

Output a single integer representing the maximum sum of any sub-rectangle in the matrix.

Example:

7
## sample
3 3
1 2 -1
-3 4 2
1 -5 3
7