QOJ.ac

QOJ

Time Limit: 6 s Memory Limit: 256 MB Total points: 100

#21184. Bull in a China Shop

Statistics

Farmer John has decided his home needs more decoration. Visiting the local china shop, he finds a delicate glass cow figurine that he decides to purchase, knowing that it will fit perfectly on the mantel above his fireplace.

The shape of the cow figurine is described by an $N \times M$ grid of characters like the one below ($3 \leq N, M \leq 500$), where lowercase letter characters are each part of the figurine (indicating different colors) and '.' characters are not.

...............
...............
x..x...........
xxxx...........
xxxxaaaaaaa...
.xx.aaaaaaaaa..
....aaaaaaa.aa.
....ll...ll....
....vv...vv....
...............

Unfortunately, right before FJ can make his purchase, a bull runs through the shop and breaks not only FJ's figurine, but many of the other glass objects on the shelves as well! FJ's figurine breaks into 3 pieces, which quickly become lost among $K$ total pieces lying on the ground ($4 \leq K \leq 100$). Each of the $K$ pieces is described by a grid of characters, just like the original figurine.

Please help FJ determine how many sets of 3 pieces (out of the $K$ on the floor) could be glued back together to mend his broken figurine.

The pieces on the ground might have been flipped vertically or horizontally, or rotated by some multiple of 90 degrees. Therefore, given the original grid as well as $K$ grids describing pieces, you want to find sets of 3 pieces that can be joined together to form the original picture, allowing the pieces to be translated, flipped, or rotated multiples of 90 degrees. When then superimposed, the 3 pieces should exactly form the original picture, with each colored square in the original picture represented in exactly one of the pieces.

Input

The first line contains a single integer $K$. Following that will be $K + 1$ piece descriptions. The first description will describe the original glass cow, the following $K$ descriptions will be of the broken pieces.

Each description begins with a line containing two integers $R$ and $C$ ($1 \le R, C \le 100$). The following $R$ lines contain $C$ lowercase alphabet characters describing the color of each cell. Each piece will be horizontally/vertically connected and have at least one non-empty cell.

Output

Output the number of triples $i, j, k$ ($i < j < k$) such that pieces $i$, $j$, and $k$ can be arranged to form the original glass cow.

Examples

Input 1

5
5 5
aaaaa
..a..
bbabb
..a..
aaaaa
3 5
..abb
..a..
aaaaa
5 2
a.
a.
aa
a.
a.
1 2
bb
1 5
bbabb
2 5
aaaaa
..a..

Output 1

3

The three solutions use pieces $(0, 1, 2)$, $(0, 2, 4)$, $(1, 3, 4)$.

Note that this problem has a time limit of 6 seconds per test case (and twice that for Java and Python submissions).

Problem credits: Brian Dean

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.