Word search puzzle

Consider an r×cr \times c board where each cell has a letter and a number that indicates the value of that cell. Given several words ww, compute the maximum number of points achievable by placing ww horizontally (to the right) or vertically (down), so that all the letters match those of the board.

Input

Input consists of several cases, each with the dimensions rr and cc, followed by rr rows with cc lowercase letters each, followed by rr rows with cc natural numbers each. Then comes a number tt followed by tt nonempty words made up of lowercase letters. You can assume that rr and cc are between 1 and 100, that the value of each cell is between 0 and 10610^6, and that the given words do not have more than 100 letters.

Output

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”.

Problem information

Author: Unknown
Translator: Salvador Roura

Generation: 2026-01-25T12:13:16.900Z

© Jutge.org, 2006–2026.
https://jutge.org