#K90627. Maximum Sunlight in a Yard
Maximum Sunlight in a Yard
Maximum Sunlight in a Yard
You are given an n × m grid where each cell contains an integer value representing the amount of sunlight that cell receives. Your task is to select a rectangular submatrix (plot) of the yard such that the sum of the sunlight values in this rectangle is maximized. The submatrix can be as small as a single cell (1×1) or as large as the entire grid.
Formulation:
Let \(G\) be the grid with dimensions \(n \times m\). For any rectangular plot defined by the top-left corner \((i_1, j_1)\) and the bottom-right corner \((i_2, j_2)\), compute the total sunlight \(S\) as:
[ S = \sum_{i=i_1}^{i_2} \sum_{j=j_1}^{j_2} G_{i,j} ]
Your goal is to find the maximum value of \(S\) over all possible rectangles.
Note: The grid values may be positive or negative.
inputFormat
The first line contains two integers, n and m (1 ≤ n, m ≤ 100), the number of rows and columns of the grid. The next n lines each contain m space-separated integers representing the grid values.
outputFormat
Output a single integer which is the maximum total sunlight that can be collected from any rectangular submatrix.## sample
2 2
1 2
3 4
10
</p>