Under construction

Still being built. Things may change

Controlled Tournament

Time limit8sMemory limit512 MB

Summary
Count knockout brackets of minimum height in which player M wins the tournament, given a fixed win relation over N up to 16 players.
Level

Hard8 of 10

Topics
Divide and conquer, Dynamic programming, Bit manipulation, Combinatorics
Solved
No attempts yet

Problem

The national tennis association is holding a competition among professional players. The competition is a knockout tournament, and arranging the bracket is your job.

You have a detailed report on every participant. It lists the results of the recent matches between all pairs of participants, and the data shows that the outcome depends only on who the two opponents are. Whenever player ii meets player jj, the same side always wins.

One of your close friends is in the competition and you want him to take the gold medal, so you want to know how many brackets let him win. There are too many participants to count by hand, so write a program that counts the brackets in which your friend wins the gold medal.

To keep the trick hidden, you cannot build a contrived bracket. The height of the tournament tree has to be as small as possible.

A bracket is a binary tree with one of the NN players at each leaf. Every internal node is one match between the winners of its two subtrees, and the player who wins at the root takes the gold medal. The height of the tree is the largest number of matches on a path from the root down to a leaf, and it must equal the smallest value a binary tree with NN leaves can reach, which is ⌈log⁡2N⌉\lceil \log_2 N \rceil. Two brackets that differ only by exchanging the two whole subtrees of some match count as one bracket.

Input

The input has several datasets. Each dataset has the following format.

N M
R11 R12 ... R1N
R21 R22 ... R2N
...
RN1 RN2 ... RNN

NN is the number of players, 2≤N≤162 \le N \le 16. MM is your friend's ID, 1≤M≤N1 \le M \le N, and players are numbered from 1. RijR_{ij} is the result of a match between player ii and player jj. If player ii always wins, Rij=1R_{ij} = 1, otherwise Rij=0R_{ij} = 0. The matrix is consistent: for every i≠ji \ne j, Rij=0R_{ij} = 0 if and only if Rji=1R_{ji} = 1. The diagonal entries RiiR_{ii} are given for convenience and are always 0.

The end of the input is a line holding two zeros. That line is not a dataset and must not be processed.

Output

For each dataset, print on one line the number of brackets in which your friend wins the first prize.

Examples2

  1. Example 1

    Input
    2 1
    0 1
    0 0
    2 1
    0 0
    1 0
    3 3
    0 1 1
    0 0 1
    0 0 0
    3 3
    0 1 0
    0 0 0
    1 1 0
    3 1
    0 1 0
    0 0 0
    1 1 0
    3 3
    0 1 0
    0 0 1
    1 0 0
    6 4
    0 0 0 0 0 1
    1 0 1 0 1 0
    1 0 0 1 1 0
    1 1 0 0 1 0
    1 0 0 0 0 0
    0 1 1 1 1 0
    7 2
    0 1 0 0 0 1 0
    0 0 1 0 1 1 1
    1 0 0 1 1 0 0
    1 1 0 0 0 1 0
    1 0 0 1 0 0 1
    0 0 1 0 1 0 0
    1 0 1 1 0 1 0
    8 6
    0 0 0 0 1 0 0 0
    1 0 1 1 0 0 0 0
    1 0 0 0 1 0 0 0
    1 0 1 0 0 1 0 1
    0 1 0 1 0 0 1 0
    1 1 1 0 1 0 0 1
    1 1 1 1 0 1 0 0
    1 1 1 0 1 0 1 0
    0 0
    
    Expected output
    1
    0
    0
    3
    0
    1
    11
    139
    78
    
  2. Example 2

    Input
    2 1
    0 1
    0 0
    2 2
    0 1
    0 0
    2 1
    0 0
    1 0
    2 2
    0 0
    1 0
    0 0
    
    Expected output
    1
    0
    0
    1