Problem B

Kriss Kross Puzzle

The Kriss Kross Puzzle is a game for a single player, where there is a puzzle grid, with black and white squares, and a set of words. Basically, Kriss Kross Puzzles are solved by placing all words into the white squares of the puzzle grid, from top to bottom or from left to right.

Consider a Kriss Kross Puzzle defined by:

A solution to the Kriss Kross Puzzle is a grid S, with the same size as G, that verifies all the following conditions.

Problem

Write a program that, given a Kriss Kross Puzzle with a unique solution, computes its solution.

Input

The first line contains an integer L (2 < L <= 20), which is the number of lines of the puzzle grid.

The second line contains an integer C (2 < C <= 20), which is the number of columns of the puzzle grid.

Each of the following L lines contains C characters. A character is:

The next line contains an integer N (4 < N <= 100), which is the size of the set of words.

Each of the following N lines contains a word. A word is a sequence of at least two capital letters, whose length does not exceed the maximum of L and C.

The list of words is known to be ordered by length (smaller words appear first).

Output

The output is the solution of the Kriss Kross Puzzle. It consists of L lines.

Each line contains C characters, which are either a capital letter, or the symbol "1" (which represents a black square).

Sample Input

4
6
010001
001000
000100
010000
12
LA
DO
NO
ON
AM
CAR
GAS
ROD
MUG
RULE
MORE
ONES

Sample Output

M1CAR1
ON1MUG
ROD1LA
E1ONES


CPN'2004: Segundo Concurso de Programação da Nova
Março de 2004
Departamento de Informática
Faculdade de Ciências e Tecnologia
Universidade Nova de Lisboa