PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000087. Đường đi của các viên bi (Jeopardy of Dropped Balls)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường đi của các viên bi (Jeopardy of Dropped Balls)

Jeopardy of Dropped Balls

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an nn by mm grid. Each cell (i,j)(i,j) contains a direction ai,ja_{i,j}: 11 means right, 22 means down, and 33 means left.

Drop kk balls one after another. Ball tt starts in the first row at column ctc_t. Each ball follows the direction of its current cell. Immediately after leaving a cell, that cell's direction changes permanently to 22 (down), affecting subsequent balls. A ball finishes after leaving the grid through the bottom.

The first column never points left and the last column never points right, so balls cannot leave through the sides. Report the column where each ball leaves the grid, taking all previous direction changes into account.

Input

The first line contains n,m,kn,m,k. The next nn lines contain mm direction values each. The last line contains c1,…,ckc_1,\ldots,c_k.

Output

Print the exit column of each of the kk balls in drop order.

Subtasks

  • Subtask 1 (20 points): 1≤n,m≤81\le n,m\le 8, 1≤k≤301\le k\le 30, 1≤ai,j≤31\le a_{i,j}\le3, 1≤ci≤m1\le c_i\le m, ai,1≠3a_{i,1}\ne3, ai,m≠1a_{i,m}\ne1.
  • Subtask 2 (30 points): 1≤n,m≤601\le n,m\le 60, 1≤k≤30001\le k\le 3000, 1≤ai,j≤31\le a_{i,j}\le3, 1≤ci≤m1\le c_i\le m, ai,1≠3a_{i,1}\ne3, ai,m≠1a_{i,m}\ne1.
  • Subtask 3 (50 points): 1≤n,m≤10001\le n,m\le 1000, 1≤k≤1051\le k\le 10^5, 1≤ai,j≤31\le a_{i,j}\le3, 1≤ci≤m1\le c_i\le m, ai,1≠3a_{i,1}\ne3, ai,m≠1a_{i,m}\ne1.

Example

Input

5 5 3
1 2 3 3 3
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
1 2 1

Output

2 2 1

Explanation

The first ball changes directions along its route and leaves at column 22. The next two balls leave at columns 22 and 11.