#P10623. Hidden Treasure Chest

    ID: 12649 Type: Default 1000ms 256MiB

Hidden Treasure Chest

Hidden Treasure Chest

Pirate Dick is tired of all the fighting and treasure hunts, so he decided to retire on a secluded island. He has a large amount of gold coins which he wants to hide in a chest submerged in a murky pond. The pond is represented by an N×M grid. Each cell in the grid has a known depth, indicating the distance from the pond’s surface (which is initially at height 0) down to the bottom at that cell. The chest is a rectangular box with an arbitrary integer height and a top with integer dimensions (rows and columns) that can be chosen arbitrarily subject to a maximum allowed size.

The chest, when placed, must be aligned with the grid. In other words, its top covers a contiguous subrectangle of the pond. Let the chosen top size be with r rows and c columns (with 1 ≤ r ≤ R and 1 ≤ c ≤ C, where R and C are the maximum allowed top dimensions given in the input, and additionally r ≤ N and c ≤ M). When Dick submerges the chest, it will sink vertically until its bottom touches the pond floor beneath at least one grid cell in the covered subrectangle. Formally, if the depths in the chosen subrectangle are given and the minimum depth among those cells is d, then the chest will sink so that its bottom is exactly at the bottom of the cell with depth d.

Let h be the integer height of the chest, and let A = r×c be the area of the chest’s top. Then the vertical position T of the chest top will be T = h - d (since the chest sinks until its bottom, at T - h, meets the pond bottom at -d). Submerging the chest displaces water equal in volume to the chest, and because the pond has a total surface area of S = N×M, the pond's surface rises uniformly by Δ = (A×h)/S.

For the chest to remain hidden, its top must be strictly below the new surface level. That is, we require:

hd<AhSh - d < \frac{A\,h}{S}

Simplifying the inequality (for the case when A < S), we obtain:

h<SdSAh < \frac{S\,d}{S - A}

Your task is to help Pirate Dick find the volume of the largest chest (i.e. the maximum value of A×h) that can be hidden in the pond.

Input Format

  • The first line contains two integers N and M (1 ≤ N, M ≤ 100), representing the number of rows and columns of the pond.
  • The next N lines each contain M positive integers. The j-th number on the i-th line represents the depth di,j (1 ≤ di,j ≤ 109) at cell (i, j) of the pond.
  • The last line contains two integers R and C (1 ≤ R ≤ N, 1 ≤ C ≤ M), representing the maximum allowed dimensions for the chest’s top (rows and columns respectively).

Output Format

  • Output a single integer, the maximum volume of a chest that can be submerged so that its top remains strictly below the water's surface after the water is displaced.

Note: If the chosen chest top covers the entire pond (i.e. when A = N×M), the water level would rise arbitrarily high making it impossible to hide the chest. Hence, you may assume that under the constraints of the input, the chest top will always cover a proper subset of the pond (i.e. A < N×M).

inputFormat

The input begins with a line containing two integers N and M. The next N lines each contain M integers denoting the depths of the pond cells. The final line contains two integers R and C, which specify the maximum allowed number of rows and columns for the chest’s top.

outputFormat

Output a single integer representing the maximum volume (i.e. top area multiplied by the chest's height) of a chest that can be hidden beneath the water surface.

sample

3 3
4 4 4
4 1 4
4 4 4
2 2
4