Counting the number of figures#11783

Counting the number of figures

1 second
256 megabytes
Medium

On a rectangular field, there are figures that do not overlap but may touch. Each figure belongs to one of types. The types of figures are given by matrices consisting of the numbers (filled cell) and (empty cell). Figures can be rotated by , , or and can also be mirrored horizontally and vertically. It is known that the figures are connected (by vertical and horizontal adjacency).

The field is given by a matrix of size H \times W and contains numbers from to , indicating the parts of the respective figures. Cells with the same numbers form one figure. All the cells that belong to one figure form a contiguous connected area (by vertical and horizontal adjacency).

Input

The first line contains an integer ( ≤ ≤ ) – the number of types of figures. Then follows the description of figures. The description of each figure begins with a line with two numbers – the width and height of the -th figure K_i \times L_i. Then there are K_i rows, each containing L_i numbers or , which define the shape of the figure. Next is a line with the dimensions of the field , ( ≤ , ≤ ). The following rows contain the H \times W matrix, which consists of numbers from (empty cell) to (figure number), where each number indicates the corresponding figure.

Output

If there are equivalent types of figures (i.e., those that can be obtained from one another by rotations or reflections), print . If there is at least one unknown figure on the field (does not match any of the given types), also print .

Otherwise, print numbers – the number of figures used of each type in the order described in the input data.

Type 1 – used 2 times (figures no. 1 and no. 4).

Type 2 – used 1 time (figure no. 2).

Type 3 – used 1 time (figure no. 3).

Examples

Example 1

Loading
Loading