#P7741. Hidden Treasure Grid
Hidden Treasure Grid
Hidden Treasure Grid
Xiaokeke has entered the palace's main hall, where the floor is made up of square stone blocks arranged in an m × n rectangular grid. Each block is either black or white (represented by 1 and 0 respectively). Under one of these stones lies a passage to the treasure vault. However, many stones are booby‐trapped so testing them one by one is dangerous.
The treasure map states that the passage is located in a specific rectangular region (with nonzero area) whose sides are parallel to the hall's boundaries. Among all possible rectangular subgrids of the floor, the chosen region is the one that maximizes the difference between the number of black stones and the number of white stones. In other words, if we denote the number of black stones by B and white stones by W, the desired region has the maximum value of
\(S = B - W = 2\times(\text{number of ones}) - (\text{area})\)
Your task is to compute this maximum possible difference S given the grid configuration.
inputFormat
The first line contains two integers, m
and n
, representing the number of rows and columns respectively.
The next m
lines each contain n
integers (each either 0 or 1) separated by spaces, representing the grid.
outputFormat
Output a single integer, the maximum difference S between the number of black and white stones in any non-empty rectangular subgrid.
sample
2 2
1 0
0 1
1
</p>