#P7059. Lucky Chances

    ID: 20265 Type: Default 1000ms 256MiB

Lucky Chances

Lucky Chances

Lucky Chances is a lottery game. Each lottery ticket consists of a rectangular \( r \times c \) grid filled with numbers. A hidden scratch area reveals a bet cell specified by its row and column indices. For the bet cell, there are 4 possible winning directions: up, down, left, and right.

You win a direction if every number in that direction (in the same row or column) from the bet cell is strictly less than the number in the bet cell. If the bet cell is on an edge of the grid, the corresponding direction is automatically won. The overall score of a ticket is the sum of the number of winning directions for every possible bet cell in the grid.

For example, if the bet cell is at position \( (i,j) \) with value \( a[i][j] \), then:

  • Up: if \( i = 0 \) (edge), win automatically; otherwise, win if \( a[k][j] < a[i][j] \) for all \( k = 0,1,\dots, i-1 \).
  • Down: if \( i = r-1 \) (edge), win automatically; otherwise, win if \( a[k][j] < a[i][j] \) for all \( k = i+1,\dots, r-1 \).
  • Left: if \( j = 0 \) (edge), win automatically; otherwise, win if \( a[i][k] < a[i][j] \) for all \( k = 0,1,\dots, j-1 \).
  • Right: if \( j = c-1 \) (edge), win automatically; otherwise, win if \( a[i][k] < a[i][j] \) for all \( k = j+1,\dots, c-1 \).

Your task is to compute the total number of winning directions over all possible bet cells in the given grid.

inputFormat

The first line contains two integers \( r \) and \( c \) (the number of rows and columns). Each of the following \( r \) lines contains \( c \) integers, representing the numbers on the grid.

outputFormat

Output a single integer which is the total number of winning directions over all possible bet cells.

sample

3 3
1 2 3
4 5 6
7 8 9
24