Codeforces Round 214 (Div. 2)
E
Dima and Magic Guitar
Codeforces
2200
原题
← D→
翻译中…

Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with $*n*$ strings and $*m*$ frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the $*i*$-th string holding it on the $*j*$-th fret the guitar produces a note, let's denote it as $*a*_{*ij*}$. We know that Dima's guitar can produce $*k*$ distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that $*a*_{*ij*} = *a*_{*pq*}$ at $(*i*, *j*) ≠ (*p*, *q*)$.

翻译中…

Dima has already written a song — a sequence of $*s*$ notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).

翻译中…

We'll represent a way to play a song as a sequence of pairs $(*x*_{*i*}, *y*_{*i*})$ $(1 ≤ *i* ≤ *s*)$, such that the $*x*_{*i*}$-th string on the $*y*_{*i*}$-th fret produces the $*i*$-th note from the song. The complexity of moving between pairs $(*x*_{1}, *y*_{1})$ and $(*x*_{2}, *y*_{2})$ equals image + image. The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.

翻译中…

Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!

翻译中…

Input

翻译中…

The first line of the input contains four integers $*n*$, $*m*$, $*k*$ and $*s*$ $(1 ≤ *n*, *m* ≤ 2000, 1 ≤ *k* ≤ 9, 2 ≤ *s* ≤ 10^{5})$.

翻译中…

Then follow $*n*$ lines, each containing $*m*$ integers $*a*_{*ij*}$ $(1 ≤ *a*_{*ij*} ≤ *k*)$. The number in the $*i*$-th row and the $*j*$-th column ($*a*_{*ij*}$) means a note that the guitar produces on the $*i*$-th string and the $*j*$-th fret.

翻译中…

The last line of the input contains $*s*$ integers $*q*_{*i*}$ $(1 ≤ *q*_{*i*} ≤ *k*)$ — the sequence of notes of the song.

翻译中…

Output

翻译中…

In a single line print a single number — the maximum possible complexity of the song.

翻译中…

样例

Input #1
4 6 5 7
3 1 2 2 3 1
3 2 2 2 5 5
4 2 2 2 5 3
3 2 2 1 4 3
2 3 1 4 1 5 1
Output #1
8
InputCopy OutputCopy
Input #2
4 4 9 5
4 7 9 5
1 2 1 7
8 3 4 9
5 7 7 2
7 1 9 2 5
Output #2
4
InputCopy OutputCopy
题目信息
题号
366E
时间限制
3 s
内存限制
256 MB
标签
暴力
模拟
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。