Consider an board where each cell has a letter and a number that indicates the value of that cell. Given several words , compute the maximum number of points achievable by placing horizontally (to the right) or vertically (down), so that all the letters match those of the board.
Input consists of several cases, each with the dimensions and , followed by rows with lowercase letters each, followed by rows with natural numbers each. Then comes a number followed by nonempty words made up of lowercase letters. You can assume that and are between 1 and 100, that the value of each cell is between 0 and , and that the given words do not have more than 100 letters.
For each word of each case, print the maximum possible score placing
the word horizontally or vertically. If the word cannot be found, print
“no”.
Input
3 4 a b c a b c a e c a b d 10 20 30 40 50 60 70 80 15 25 35 45 3 bca cabb a 1 1 z 1000000 2 y z
Output
180 no 70 no 1000000