Problem:
Problem F: Controlled Tournament

National Association of Tennis is planning to hold a tennis competition among professional
players. The competition is going to be a knockout tournament, and you are assigned the task
to make the arrangement of players in the tournament.

You are given the detailed report about all participants of the competition. The report contains
the results of recent matches between all pairs of the participants. Examining the data, you’ve
noticed that it is only up to the opponent whether one player wins or not.

Since one of your special friends are attending the competition, you want him to get the best
prize. So you want to know the possibility where he wins the gold medal. However it is not so
easy to figure out because there are many participants. You have decided to write a program
which calculates the number of possible arrangements of tournament in which your friend wins
the gold medal.

In order to make your trick hidden from everyone, you need to avoid making a factitive tourna-
ment tree. So you have to minimize the height of your tournament tree.

Input

The input consists of multiple datasets. Each dataset has the format as described below.

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

N (2 ≤ N ≤ 16) is the number of player, and M (1 ≤ M ≤ N) is your friend’s ID (numbered
from 1). Rij is the result of a match between the i-th player and the j-th player. When i-th
player always wins, Rij = 1. Otherwise, Rij = 0. It is guaranteed that the matrix is consistent:
for all i ≠ j, Rij = 0 if and only if Rji = 1. The diagonal elements Rii are just given for
convenience and are always 0.

The end of input is indicated by a line containing two zeros. This line is not a part of any
datasets and should not be processed.

Output

For each dataset, your program should output in a line the number of possible tournaments in
which your friend wins the first prize.

Sample 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

Output for the Sample Input

1
0
0
3
0
1
11
139
78

Solution in C++ (judge verdict: Accepted)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define repi(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,a) repi(i,0,a)
#define repd(i,a,b) for(int i = (a) ; i >= (b); i--)
#define repit(i,a) for(__typeof((a).begin()) i = (a).begin(); i!= (a).end(); i++)
#define all(u) (u).begin(),(u).end()
#define rall(u) (u).rbegin(),(u).rend()
#define UNIQUE(u) (u).erase(unique(all(u)),(u).end)

#define pb push_back
#define mp makek_pair
#define INF 1e9
#define EPS 1e-9
#define PI acos(-1.0)

using namespace std;

typedef long long ll;
typedef vector<int> vi;

int N, M, R[16][16], mem[1<<16][16][8];

int f(int s, int i, int h)
{
    if (mem[s][i][h] >= 0) return mem[s][i][h];
    if (!(s >> i & 1)) return 0;
    int sz = __builtin_popcount(s);
    if (sz == 1) return 1;

    int ret = 0;
    int M = 1 << h - 1;
    int m = sz - M;
    int tmp = s ^ 1 << i;
    for (int t = tmp; t; --t, t &= tmp) {
	int k = __builtin_popcount(t);
	if (m <= k && k <= M)
	    for (int j = 0; j < N; ++j)
		if (R[i][j])
		    ret += f(s ^ t, i, h - 1) * f(t, j, h - 1);
    }
    return mem[s][i][h] = ret;
}

void solve()
{
    memset(mem, -1, sizeof(mem));

    --M;
    rep(i,N) rep(j,N) cin >> R[i][j];

    cout << f((1 << N) - 1, M, 32 - __builtin_clz(N - 1)) << endl;
}

int main()
{
    while (cin >> N >> M && N)
	solve();

    return 0;
}