1754: Paper Cut

内存限制:128 MB 时间限制:2.000 S
评测方式:文本比较 命题人:
提交:0 解决:0

题目描述

Recently Raven is addicted to paper cutting games. There is a rectangle-shape paper. Its length is N, and its width is M. On the face side, there are letters in each cell (capital letters A-Z). There are scores on the back side of each letter. A word can be made up of adjacent letters in the paper. Note that adjacency is defined as one of the four following directions only (left, right, up and down). Now a word will be given to you. Please cut this word in the paper to get the highest score of the word. The score of the word is the sum of the score of the letters in the word.

输入

The first line of the input contains an integer T, which indicates the number of test cases. In each case, the first row of each case has two integer: N M, ( 0 < N , M <= 500 ) In the following N rows, each row has one string (containing M characters). In the next N rows, each row has M integers divided by space; represent the score of the corresponding letter. The last row has a string (no more than 26 characters) represents the required word to be cut. Input will make sure there are no repeated letters.

输出

For each case, output one integer S representing the highest score. If you cannot cut this kind of words, just output -1.

样例输入 复制

1
3 4
ABCD
BCDE
CDEF
1 1 1 1
1 1 2 1
1 1 1 1
ABCD

样例输出 复制

5