#P15892. [COCI 2025/2026 #6] Učionica

[COCI 2025/2026 #6] Učionica

Description

A group of kk friends enters a classroom. The height hih_i of each friend is known, and the heights of all other students already in the classroom are also known.

The classroom can be represented as an n×mn \times m grid aa, where each cell corresponds to a seat. The seat in the ii-th row and jj-th column is denoted by ai,ja_{i,j}. For every seat, we know whether it is already occupied by a student (along with their height) or if it is empty.

The kk friends want to take seats in the classroom. Each friend occupies exactly one empty seat, and no two friends can sit in the same seat. Additionally, they want to sit next to each other in the same row. More precisely, they choose a row index ii such that 1in1 \le i \le n and a column index jj such that 1jmk+11 \le j \le m - k + 1, and sit in the seats ai,j,ai,j+1,,ai,j+k1a_{i,j}, a_{i,j+1}, \dots, a_{i,j+k-1}. The friends can sit in these seats in any order; they do not have to sit in the order in which their heights were given.

A friend considers a seat suitable only if all students sitting in front of them (i.e., in rows with a smaller index in the same column) are strictly shorter than them, ensuring they have a clear view. The group considers a set of kk consecutive seats in the same row suitable if they can arrange themselves so that each friend has a clear view.

Considering these conditions, how many suitable sets of seats for the group are there in the classroom?

Input Format

The first line contains three natural numbers nn, mm, and kk (1n,m2000,1km1 \le n, m \le 2000, 1 \le k \le m) - the number of rows and columns of the classroom and the number of friends.

The second line contains kk natural numbers h1,h2,,hkh_1, h_2, \dots, h_k - the heights of the friends.

The next nn lines each contain mm natural numbers: if ai,j=0a_{i,j} = 0, the seat is empty, and if ai,j1a_{i,j} \ge 1, the seat is occupied by a student of height ai,ja_{i,j} (1ai,j1091 \le a_{i,j} \le 10^9).

Output Format

In the first and only line, output a single number - the number of suitable sets of kk consecutive seats in the same row where the friends can be arranged so that each has a clear view.

3 4 2
2 6
0 0 3 1
8 0 0 0
0 0 1 0
3
2 4 4
5 2 4 3
1 2 3 4
0 0 0 0
1
5 5 3
17 3 17
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
15

Hint

Clarification of the first example:
The two friends can sit in the first and second seats in the first row, in the second and third seats in the second row, or in the third and fourth seats in the second row. So, they can sit in a total of 33 different sets of seats.

Clarification of the second example:
The four friends can sit in the only four available seats if they arrange themselves from shortest to tallest.

Clarification of the third example:
The three friends can sit in any three consecutive seats in the same row since there are no other students in the classroom resulting in a total of 1515 valid sitting arrangements.

Scoring

Subtask Points Constraints
1 11 k2k \le 2
2 13 n,m200n, m \le 200
3 29 n,m500n, m \le 500
4 57 No additional constraints.