New Fountain
InterviewTime limit2sMemory limit1024 MB
Given an n by m grid of pillar heights, find the maximum total volume of water the grid can hold without draining off the border.
Statement
Today Lorelin was walking through Alfa when she found a new fountain that will open soon and be filled with water. She wondered how much water the fountain can hold.
The fountain is a set of pillars. Its base is an rectangular grid divided into unit squares. Rows are numbered 1 to and columns 1 to . Each cell holds a pillar of positive integer height, and the pillar in row , column has height .
When the fountain opens, water pours in from above and fills every empty space inside it. Formally, once the fountain opens, each pillar gets a column of water of some non-negative height, possibly zero. Let be the water height on the pillar in row , column . Water does not flow out of the fountain if these conditions hold:
- If or , then . In other words, pillars on the border carry no water, and water drains out of the fountain.
- If , then either or . In other words, a pillar that holds water has a total height no greater than each of its side neighbors.
The total water volume is the sum of all . The fountain fills so that this total is as large as possible.
Lorelin remembers the base size and every pillar height, but she cannot work out the volume. Help her compute it.
Input
The first line contains two integers and , the number of rows and columns of the base (). Each of the next lines contains integers. Line contains , the heights of the pillars in row ().
Output
Print a single number: the maximum total volume of water that can fit in the fountain.
Hint
In the first test, the maximum volume is reached with , , and for all other pillars. These values clearly satisfy all the conditions, and the total volume is 3.
In the second test, only two pillars can hold water. The maximum volume is reached with , , and for all other pillars. The total volume is 2.