#P8217. [THUPC 2022 初赛] 数正方体

[THUPC 2022 初赛] 数正方体

Problem Description

Xiao E has a rectangular area of size n×mn\times m. It contains n×mn\times m unit grid cells. On the cell in row ii and column jj, there are Ai,jA_{i,j} identical cube blocks stacked.

After solving a certain problem, Xiao E suddenly wanted to draw these cubes as ASCII art, and asks you to help him count how many cubes he has in total. We define each cube in the following format. There will be no rotation; cubes are placed strictly in this one form:

..+---+
./   /| height
+---+ |
|   | +
|   |/. width
+---+..
length

Each vertex is represented by one +. The length is represented by three -. The width is represented by one /. The height is represented by two |. The character . is used as the background. The blank area in the middle is a space (ASCII code 3232).

If two cubes are adjacent left to right, they look like:

..+---+---+
./   /   /|
+---+---+ |
|   |   | +
|   |   |/.
+---+---+..

If two cubes are adjacent up and down, they look like:

..+---+
./   /|
+---+ |
|   | +
|   |/|
+---+ |
|   | +
|   |/.
+---+..

If two cubes are adjacent front to back, they look like:

....+---+
.../   /|
..+---+ |
./   /| +
+---+ |/.
|   | +..
|   |/...
+---+....

The faces of the cube in front will block the faces of the cube behind. To make sure you can see clearly and that no entire column of cubes is completely blocked behind, Xiao E guarantees that 1AijAi1,j1\le A_{ij} \le A_{i-1,j} and 1AijAi,j11\le A_{ij}\le A_{i,j-1}. Also, there is no entire row or entire column of . in the picture.

Therefore, one ASCII art corresponds to a unique matrix AA, and one matrix AA also corresponds to a unique ASCII art.

Input Format

The first line contains two positive integers r,cr,c, meaning the height and width of the picture (note that these are not nn and mm).

Next comes an ASCII art of rr rows and cc columns, describing the cubes stacked by Xiao E.

Output Format

Output one integer in one line, representing the number of cubes.

14 17
....+---+---+....
.../   /   /|....
..+---+---+ |....
./   /|   | +---+
+---+ |   |/   /|
|   | +---+---+ |
|   |/   /|   | +
+---+---+ |   |/|
|   |   | +---+ |
|   |   |/   /| +
+---+---+---+ |/.
|   |   |   | +..
|   |   |   |/...
+---+---+---+....
14

Hint

[Sample Explanation]

At this time, the matrix AA is

$$\begin{bmatrix}3 & 3 & 2 \\ 3 & 2 & 1\end{bmatrix}$$

Since 3+3+3+2+2+1=143+3+3+2+2+1=14, there are 1414 cubes in the picture.

[Constraints]

It is guaranteed that 1n,m501\le n,m \le 50 and 1Aij1001\le A_{ij}\le 100 (note that here these are nn and mm, not rr and cc).

It is guaranteed that 1<in\forall 1<i\le n, AijAi1,jA_{ij}\le A_{i-1,j}.

It is guaranteed that 1<j<m\forall 1<j<m, AijAi,j1A_{ij}\le A_{i,j-1}.

It is guaranteed that in the ASCII art, there is no entire row or entire column that is ..

Xiao E says: Solving this problem is not hard. However, it is still suggested that you read the editorial after getting AC on this problem.

Translated by ChatGPT 5