This page is still under construction.

Parts of this page are still being built. What you see may change.

Sandcastle 2

Time limit4sMemory limit1024 MB

Summary
Count the rectangles whose cells can be visited by a path that steps only to adjacent cells with strictly lower heights.
Level

Hard9 of 10

Topics
Sorting, Divide and conquer, Implementation
Solved
No attempts yet

Problem

JOI-kun is playing on a sand beach and makes a sandcastle. The sandcastle lies in a rectangular region of sand made of HH rows and WW columns of cells. The cell in the ii-th row from the north and the jj-th column from the west has height Ai,jA_{i,j}. All heights are different from each other.

JOI-kun performed the following actions. First, he chose a cell and started moving from it. Then he moved from the current cell to an adjacent cell in one of the four directions. The cell he moved to had to be lower than the current cell. He repeated this zero or more times.

Finally, if you view the cells he visited from above, they form a rectangle. Count the number of possible rectangles formed by the cells JOI-kun visited.

Input

The first line contains two integers HH and WW. Each of the next HH lines contains WW integers, where the jj-th integer on the ii-th line is Ai,jA_{i,j}.

Output

Print one line containing the number of possible rectangles formed by the cells JOI-kun visited.

Constraints

H≥1H \ge 1, W≥1W \ge 1, H×W≤50 000H \times W \le 50\,000, and 1≤Ai,j≤10 000 0001 \le A_{i,j} \le 10\,000\,000 for all ii and jj. Any two different cells have different heights.

Examples3

  1. Example 1

    Input
    1 5
    2 4 7 1 5
    
    Expected output
    10
    
  2. Example 2

    Input
    3 2
    18 10
    19 12
    17 13
    
    Expected output
    15
    
  3. Example 3

    Input
    3 5
    83 47 36 38 40
    13 10 26 68 67
    15 19 20 70 90
    
    Expected output
    65