#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<stdlib.h>
using namespace std;		
const int MaxM = 64;    	/* 总共有m列需要被覆盖 	*/
const int MaxN = 64;		/* 总共有n行来进行覆盖，从中挑选K行 */
const int maxnode = 4096;  /* 最大点数 V = N * M */
int K;  /* 选取K行进行覆盖 */
struct DLX
{
    int n,m,size;
    int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];  
    int H[MaxN],S[MaxN];   
    void init(int _n,int _m) /* 初始化 */
    {
        n = _n;		/* 可选取的行 */
        m = _m; 	/* 被覆盖的列 */
        for(int i = 0;i <= m;i++)
        {
            S[i] = 0;
            U[i] = D[i] = i;
            L[i] = i-1;
            R[i] = i+1;
        }
        R[m] = 0; L[0] = m;
        size = m;
        for(int i = 1;i <= n;i++)
            H[i] = -1;
    }
    void Link(int r,int c)  /* 建边， 如果 matrix[r][c]=1,则调用函数  link(r,c) 来创建对应链表 */
    {
        ++S[Col[++size]=c];
        Row[size] = r;
        D[size] = D[c];
        U[D[c]] = size;
        U[size] = c;
        D[c] = size;
        if(H[r] < 0)	H[r] = L[size] = R[size] = size;
        else
        {
            R[size] = R[H[r]];
            L[R[H[r]]] = size;
            L[size] = H[r];
            R[H[r]] = size;
        }
    }
    void remove(int c)
    {
        for(int i = D[c];i != c;i = D[i])
            L[R[i]] = L[i], R[L[i]] = R[i];
    }
    void resume(int c)
    {
        for(int i = U[c];i != c;i = U[i])
            L[R[i]]=R[L[i]]=i;
    }
    bool v[maxnode];   /* 用来处理估价函数 */
    int f()   /* 估价函数   判断还剩下多少完全独立的列 */
    {
        int ret = 0;
        for(int c = R[0];c != 0;c = R[c])	v[c] = true;
        for(int c = R[0];c != 0;c = R[c])
            if(v[c])
            {
                ret++;
                v[c] = false;
                for(int i = D[c];i != c;i = D[i])	/* 枚举该列中包含的所有可选行，并将那些行中包含的列全部删除 */
                    for(int j = R[i];j != i;j = R[j])
                        v[Col[j]] = false;
            }
        return ret;

    }
    bool Dance(int d)	/* return 1 success  return 0 failed */
    {
        if(d + f() > K)return false; 
        if(R[0] == 0)return d <= K;
        int c = R[0];
        for(int i = R[0];i != 0;i = R[i])
            if(S[i] < S[c])
                c = i;
        for(int i = D[c];i != c;i = D[i])
        {
            remove(i);
            for(int j = R[i];j != i;j = R[j])remove(j);
            if(Dance(d+1))	return true;
            for(int j = L[i];j != i;j = L[j])resume(j);
            resume(i);
        }
        return false;
    }
};
DLX g;