#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#define nmax_def 110000
#define Cmax_def 2200
#define mod 1000000007
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define graph_from1 true
#define graph_tree false
#define graph_weight false
#define graph_digraph false
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;

class Graph_func
{
public:
	int Vnum;
	int ENum;
	vector<pair<int, int> > edgelist;
	vector<ll> edgeweight;
	vector<vector<int> > adjlist;
	vector<vector<ll> > adjlistweight;
	vector<int> adjnum;

	vector<ll> xdist;

	vector<bool> x_visited;
	vector<int> x_prqarr;
	vector<bool> x_filled;

	void read_edge();
	void adjini();
	void visited_ini();
	void adjsort();
	vector<bool> graph_num_BFS(int BFS_ini, vector<bool> filled);
	int graph_num();
	void graph_BFS(ll BFS_ini);
	void graph_DFS(ll DFS_ini);
	void xydist_fill(int x, ll dist_tmp);
	void xydist_tree(int x, ll dist_tmp);   // ex. xydist_tree(3, 0)
	void xydist(int x);   // ex. xydist(3, 0)
	ll diameter();
	int leaves_num();

	bool xydist_def = false;
	bool can = true;
};

void Graph_func::read_edge()
{
	cin >> Vnum;
	ENum = 0;
	rep(i, Vnum) {
		rep(j, Vnum) {
			char c;
			cin >> c;
			if (i > j)
				continue;
			if (c == '0')
				continue;
			edgelist.push_back(make_pair(i, j));
			ENum++;
		}
	}
	edgeweight.resize(ENum);
	adjnum.resize(Vnum);
	xdist.resize(Vnum);
	adjlist.resize(Vnum);
	return;
}

void Graph_func::adjini()
{
	for (int i = 0; i < Vnum; i++)
		adjnum[i] = 0;
	for (int i = 0; i < ENum; i++)
	{
		int x = edgelist[i].first;
		int y = edgelist[i].second;
		adjlist[x].push_back(y);
		if (!graph_digraph)
			adjlist[y].push_back(x);
		if (graph_weight)
		{
			ll xyweight = edgeweight[i];
			adjlistweight[x].push_back(xyweight);
			if (!graph_digraph)
				adjlistweight[y].push_back(xyweight);
		}
		adjnum[x]++;
		if (!graph_digraph)
			adjnum[y]++;
	}
	x_visited.resize(Vnum);
	for (int i = 0; i < Vnum; i++)
		x_visited[i] = false;

	return;
}

void Graph_func::visited_ini()
{
	for (int i = 0; i < Vnum; i++)
		x_visited[i] = false;
}

void Graph_func::adjsort()
{
	for (int i = 0; i < Vnum; i++)
		sort(adjlist[i].begin(), adjlist[i].end(), less<int>());
	return;
}

vector<bool> Graph_func::graph_num_BFS(int BFS_ini, vector<bool> filled) // ex. graph_num_BFS(i, filled);
{
	x_prqarr.push_back(BFS_ini);
	x_visited[BFS_ini] = true;

	while (!x_prqarr.empty())
	{
		int Ntmp = x_prqarr.size();
		for (int a = 0; a < Ntmp; a++)
		{
			int x = x_prqarr[0];
			x_prqarr.erase(x_prqarr.begin());
			for (int i = 0; i < adjnum[x]; i++)
			{
				int x_new = adjlist[x][i];
				if (!x_visited[x_new])
				{
					x_prqarr.push_back(x_new);
					x_visited[x_new] = true;
					filled[x_new] = true;
				}
			}
		}
	}
	return filled;
}

int Graph_func::graph_num()
{
	int anstmp = 0;
	int i = 0;
	vector<bool> filled;
	filled.resize(Vnum);
	for (int j = 0; j < Vnum; j++)
		filled[j] = false;
	bool end = false;
	while (!end)
	{
		if (i >= Vnum)
			break;
		while (1)
		{
			if (!filled[i])
				break;
			i++;
			if (i >= Vnum)
			{
				end = true;
				break;
			}
		}
		if (end)
			break;
		filled[i] = true;
		filled = graph_num_BFS(i, filled);
		anstmp++;
	}
	return anstmp;
}

void Graph_func::graph_BFS(ll BFS_ini)
{
	x_prqarr.push_back(BFS_ini);
	x_visited[BFS_ini] = true;

	while (!x_prqarr.empty())
	{
		int Ntmp = x_prqarr.size();
		for (int a = 0; a < Ntmp; a++)
		{
			int x = x_prqarr[0];
			x_prqarr.erase(x_prqarr.begin());
			for (int i = 0; i < adjnum[x]; i++)
			{
				int x_new = adjlist[x][i];
				if (!x_visited[x_new])
				{
					x_prqarr.push_back(x_new);
					x_visited[x_new] = true;
					cout << x_new << endl;
				}
			}
		}
	}
	return;
}

void Graph_func::graph_DFS(ll DFS_ini)
{
	for (int i = 0; i < Vnum; i++)
		x_visited[i] = false;
	x_prqarr.push_back(DFS_ini);
	x_visited[DFS_ini] = true;
	xdist[DFS_ini] = 0;

	while (!x_prqarr.empty())
	{
		int Ntmp = x_prqarr.size();
		for (int a = 0; a < Ntmp; a++)
		{
			int x = x_prqarr[x_prqarr.size() - 1];
			x_prqarr.pop_back();
			for (int i = 0; i < adjnum[x]; i++)
			{
				int x_new = adjlist[x][i];
				if (!x_visited[x_new])
				{
					x_visited[x_new] = true;
					x_prqarr.push_back(x_new);
					xdist[x_new] = xdist[x] + 1;
				}
				else
				{
					if (abs(xdist[x_new] - xdist[x] + 1) % 2 == 1)
						can = 0;
				}
			}
		}
	}
	return;
}

void Graph_func::xydist_fill(int x, ll dist_tmp)
{
	xdist[x] = dist_tmp;
	x_visited[x] = true;
}

void Graph_func::xydist_tree(int x, ll dist_tmp)  // ex. xydist_tree(3, 0), DFS
{
	if (!xydist_def)
	{
		xydist_def = true;
		xydist_fill(x, 0);
	}
	for (int i = 0; i < adjnum[x]; i++)
	{
		int x_new = adjlist[x][i];
		if (!x_visited[x_new])
		{
			xydist_fill(x_new, dist_tmp + 1);
			xydist_tree(x_new, dist_tmp + 1);
		}
	}
	return;
}

void Graph_func::xydist(int x)  // ex. xydist(3), Dijkstra's algorithm
{
	priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > xdist_que; //Alive_vertices
	ll max_tmp = pow(10, 18) * 4;
	for (int i = 0; i < Vnum; i++)
		xdist[i] = max_tmp;
	xdist[x] = 0;
	pair<ll, int> xdist_que_tmp;
	xdist_que_tmp.first = 0;
	xdist_que_tmp.second = x;
	xdist_que.push(xdist_que_tmp);
	while (!xdist_que.empty())
	{
		xdist_que_tmp = xdist_que.top();
		xdist_que.pop();
		ll xdist_tmp = xdist_que_tmp.first;
		int x_tmp = xdist_que_tmp.second;
		if (xdist[x_tmp] == xdist_tmp)
		{
			for (int i = 0; i < adjnum[x_tmp]; i++)
			{
				int x_new = adjlist[x_tmp][i];
				ll dist_diff;
				if (graph_weight)
					dist_diff = adjlistweight[x_tmp][i];
				else dist_diff = 1;
				if (xdist[x_new] > xdist[x_tmp] + dist_diff)
				{
					xdist[x_new] = xdist[x_tmp] + dist_diff;
					xdist_que_tmp.first = xdist[x_new];
					xdist_que_tmp.second = x_new;
					xdist_que.push(xdist_que_tmp);
				}
			}
		}
	}
}

int Graph_func::leaves_num() // included adjini()
{
	int anstmp = 0;
	for (int i = 0; i < Vnum; i++)
		adjnum[i] = 0;
	for (int i = 0; i < ENum; i++)
	{
		int x = edgelist[i].first;
		int y = edgelist[i].second;
		adjlist[x].push_back(y);
		adjlist[y].push_back(x);
		adjnum[x]++;
		adjnum[y]++;
	}
	for (int i = 0; i < Vnum; i++)
	{
		if (adjnum[i] == 1)
			anstmp++;
	}
	return anstmp;
}

int main(void)
{
	Graph_func graph;
	ll ans=0;
	graph.read_edge();
	graph.adjini();
	rep(i, graph.Vnum) {
		graph.xydist(i);
		rep(j, graph.Vnum)
			ans = max(ans, graph.xdist[j]);
	}
	ans++;
	rep(i, graph.Vnum) {
		graph.graph_DFS(i);
	}
	if (!graph.can)
		ans = -1;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 764