Under construction

Still being built. Things may change

Paper Presentation

Time limit4sMemory limit1024 MB

Summary
Count the orderings of 2M papers split into two slots of M each, where every dependent scientist must share a slot with the scientist they depend on.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation, Graph
Solved
No attempts yet

Problem

2M scientists are scheduled to present papers at a conference in one day. The day is divided into two slots, a morning slot and an evening slot, and a lunch break separates them. M scientists present in the morning slot and the remaining M present in the evening slot.

Some scientists depend on papers from other scientists being presented before theirs. For example, if scientist A presents "Graph Theory" and scientist B presents "Max flow-Min cut", then A must present before B. The lunch break is a time for socializing and partying, and attendees tend to forget the papers from the previous slot. For this reason, a dependent scientist (B in this example) must present in the same slot as the scientist they depend on (A in this example). Given the dependencies, find the number of possible orderings of the presentations.

Input

The first line contains an integer TT (T≤20T \le 20), the number of test cases.

Each test case starts with an integer MM (1≤M≤81 \le M \le 8). The next 2M2M lines each contain 2M2M characters, either 'Y' or 'N'. If the jj-th character of the ii-th line is 'Y', then scientist ii depends on scientist jj. If it is 'N', there is no dependence. No scientist depends on themselves.

Output

For each test case, print one line containing the number of possible orderings of the scientists.

Examples1

  1. Example 1

    Input
    3
    2
    NNNN
    NNNN
    NNNN
    NNNN
    2
    NYNN
    NNNN
    NNNY
    NNNN
    2
    NYYY
    YNYY
    YYNY
    YYYN
    
    Expected output
    24
    2
    0