Do you like assembling jigsaw puzzles? Even if you do not, that is fine. In this problem your task is not to assemble the puzzle yourself, but to count how many different correct assemblies are possible.
You are given n×m puzzle pieces. Each piece is a square with four sides. Every side is one of three kinds:
Two pieces may be joined along a shared side only if one of the two touching sides is a tab and the other is a blank (the tab fits into the blank). Pieces may be rotated freely by multiples of 90∘, but may not be flipped over.
The pieces are assembled correctly when they form an n×m rectangle and, at every place where two pieces meet, one side is a tab and the other is a blank. A side lying on the outer border of the rectangle touches no other piece, so every border side must be flat.
All pieces have the same color, so two assemblies are considered different only when the pattern of tabs and blanks over the whole rectangle differs. The orientation of the rectangle is fixed, so two assemblies are counted as different even if one can be obtained by rotating the other.
Count the number of different correct assemblies.
The first line contains the number of tests d (1≤d≤100).
Each test begins with a line containing two integers n and m (1≤n≤6, 1≤m≤5). The next n×m lines describe the pieces. Each piece is given by four integers listing the kinds of its four sides in clockwise order, where 0 is a flat side, 1 is a tab, and 2 is a blank (hole).
For each test, print on its own line the number of different correct assemblies.
The figure below shows one correct way to assemble a set of pieces.
