#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

LL comb(LL n, LL k)
{
	k = min(k, n-k);

	LL c = 1;
	for(LL i=0; i<k; ++i)
		c *= n-i, c /= i+1;
	return c;
}

class StarsInGraphs { public:
	int starnum(LL n, LL C)
	{
		if( n<=2 )
			return -1;
		LL sum = 0;
		for(LL k=3; k<=n; ++k)
		{
			sum += comb(n,k);
			if( sum > C )
				return -1;
		}
		return (int) sum;
	}

	int starryPaths(vector <string> adjacencyMatrix, int C)
	{
		const int N = adjacencyMatrix.size();

		vector<int> sn;
		for(int i=0; i<N; ++i)
			sn.push_back( starnum(count(adjacencyMatrix[i].begin(), adjacencyMatrix[i].end(), '1'), C) );

		vector<int> v;
		for(int i=0; i<N; ++i)
			if( sn[i] > 0 )
				v.push_back(i);

		for(int step=0 ;; ++step)
		{
			if( v.empty() )
				return step;
			if( step >= 3*N )
				break; // infinite

			vector<int> v2;
			for(int u=0; u<N; ++u)
				for(int i=0; i<v.size(); ++i)
					if( adjacencyMatrix[v[i]][u]=='1' && sn[v[i]] <= sn[u] )
						{v2.push_back(u); break;}
			v.swap(v2);
		}

		return -1;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, StarsInGraphs().starryPaths(adjacencyMatrix, C));}
int main(){

CASE(0)
	string adjacencyMatrix_[] = {"01110",
 "10111",
 "00000",
 "00000",
 "00000"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 1000; 
	int _ = 2; 
END
CASE(1)
	string adjacencyMatrix_[] = {"01011",
 "00111",
 "10011",
 "00000",
 "00000"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 1; 
	int _ = -1; 
END
CASE(2)
	string adjacencyMatrix_[] = {"0111",
 "0000",
 "0000",
 "0000"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 1; 
	int _ = 1; 
END
CASE(3)
	string adjacencyMatrix_[] = {"01111",
 "00000",
 "00000",
 "00000",
 "00000"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 4; 
	int _ = 0; 
END
CASE(4)
	string adjacencyMatrix_[] = {"010001100",
 "001001100",
 "000101110",
 "000010111",
 "000001111",
 "010000000",
 "000110000",
 "000100001",
 "100001000"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 10; 
	int _ = 5; 
END
CASE(5)
	string adjacencyMatrix_[] = {"00","00"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 9999999; 
	int _ = 0; 
END
CASE(6)
	string adjacencyMatrix_[] = {"0111","1011","1101","1110"};
	  vector <string> adjacencyMatrix(adjacencyMatrix_, adjacencyMatrix_+sizeof(adjacencyMatrix_)/sizeof(*adjacencyMatrix_)); 
	int C = 9999; 
	int _ = -1; 
END

}
// END CUT HERE
