#K4656. Non-empty Grid Positions After One Move

    ID: 28003 Type: Default 1000ms 256MiB

Non-empty Grid Positions After One Move

Non-empty Grid Positions After One Move

You are given a grid consisting of n rows and m columns. Each cell initially contains an item. In one move, every item simultaneously shifts once: it may either stay in its original cell or move to one of its adjacent cells (up, down, left, or right), provided that the move does not take the item outside the grid. Multiple items are allowed to move into the same cell.

Your task is to determine the total number of distinct positions in the grid that will be non-empty after the move. Even if an item moves from one cell into another cell that already had an item (or vice versa), count that cell only once.

Formally, for each cell \((i,j)\) with \(0 \leq i < n\) and \(0 \leq j < m\), an item can stay in \((i,j)\) or move to any adjacent cell \((i-1,j)\), \((i+1,j)\), \((i,j-1)\), or \((i,j+1)\) if such cell exists. Compute the number of unique grid positions that could contain an item after all moves.

The mathematical formulation for checking valid positions is:

[ P = { (i,j) \mid 0 \leq i < n, 0 \leq j < m } \cup { (i-1,j), (i+1,j), (i,j-1), (i,j+1) \mid (i,j) \in P \text{ and the coordinate is valid} } ]

inputFormat

The first line contains two integers \(n\) and \(m\) separated by a space, representing the number of rows and columns of the grid.

The following \(n\) lines each contain \(m\) space-separated integers. These integers represent the grid cells. Note that the values in the grid are not used in the computation, but are provided for consistency.

outputFormat

Output a single integer: the number of distinct positions in the grid that may contain at least one item after the movement.

## sample
3 3
1 2 3
4 5 6
7 8 9
9