//In the name of GOD
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#define MAX 35
using namespace std;
int numparts=0,m,n,ans[MAX][MAX],numans,part[2][MAX],M,N;

ofstream fout;

string ncast(int x){
	string s;
	if(!x)
		return "0" ;
	for (;x; s=char((x%10)+'0')+s , x/=10);
	return s;
}

const char* Filename(int x,int y,string t){
	string s=ncast(x)+t+ncast(y)+(string)(".txt");
	const char *filename=s.c_str();
	return filename;
}

void print(){
	numans++;
	for (int i=0;i<m;i++ , fout << endl)
		for (int j=0;j<n;j++)
			fout << ans[i][j] << ' ';
	fout << endl;
}

bool r[MAX][MAX];
void bt(int d,int f){
	if (d==numparts){
		print();
		return ;
	}
	if (part[1][d]==0){
		bt(1,0);
		return ;
	}
	ifstream fin (Filename(part[0][d],part[1][d]-1,"comp"));
	int mat=0;
	if (part[1][d]!=1)
		fin >> mat;
	int t1=0,t2=0;
	for (int dd=0;dd<d;dd++){
		t1+=part[0][dd];
		t2+=part[1][dd];
	}
	for (int i=0;i<m;i++)
		for (int j=0;j<part[1][d];++j)
			ans[i][j+t2]=0;
	for (int i=0;i<part[0][d];++i)
		ans[i+t1][t2]=1;
	for (int k=0;k<mat;k++){
		for (int i=0;i<part[0][d];i++)
			for (int j=0;j<part[1][d]-1;++j)
				fin >> r[i][j];
		if (k<f && d && part[0][d]==part[0][d-1] && part[1][d]==part[1][d-1])
			continue;
		for (int i=0;i<part[0][d];i++)
			for (int j=0;j<part[1][d]-1;j++)
				ans[t1+i][t2+1+j]=r[i][j];
		bt(d+1,k);
	}
	if (part[1][d]==1)
		bt(d+1,0);
}

bool check(){
	for (int i=0;i<numparts;++i)
		if (part[0][i]>m/2)
			return false;
	return true;
}

void solve(){
	if (m!=M || n!=N || check())
		bt(0,0);
}


void partition(int x,int y,int maxx,int maxy){
	if (!x && !y)
		solve();
	if (!x  || !y || !maxy || x>y)
		return ;
	if (maxy<=y)
		for (int i=1;i<=maxx && i<=x && i<=maxy;++i){
			part[0][numparts]=i;
			part[1][numparts]=maxy;
			numparts++;
			partition(x-i,y-maxy,i,maxy);
			numparts--;
		}
	partition(x,y,x,maxy-1);
}

void Total_Partition(int x,int y){
	fout.open("output.txt");
	numans=0;
	m=x;n=y;
	numparts=0;
	partition(x,y,x,y);
	part[0][0]=1;
	part[1][0]=0;
	numparts=1;
	partition(x-1,y,x-1,y);
	fout.close();
	ifstream in ("output.txt");
	ofstream out (m==M && n==N ? Filename(m,n,"result") : Filename(m,n,"comp"));
	out << numans << endl;
	for (int i=0;i<numans;i++ , out << endl)
		for (int j=0;j<m;j++,out << endl)
			for (int k=0;k<n;k++){
				bool bl;
				in >> bl;
				out << bl << ' ';
			}
	in.close();
	out.close();
}



int main(){
	cout << "Enter the number of different rows(Haplotypes)"<<endl;
	cin >> M ;
	cout << "Enter the number of different columns(SNPs)"<<endl;
	cin >> N ;
	
	if (M>N+1 || M==1 && N==1){
		ofstream out (Filename(M,N,"result"));
		out << 0 << endl;
		out.close();
		return 0;
	}
	Total_Partition(1,1);
	Total_Partition(2,1);
	for (int i=2;i<=N;++i)
		for (int j=1;j<=i+1 && j<=M/2;++j)
			Total_Partition(j,i);
	Total_Partition(M,N);
}

