1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif

#define S 407

int dist[S][S];
bitset<S> wrong[S];

void clear(int n) {
	for(int i = 1; i <= n; i++) {
		dist[i][i] = 0;
		for(int j = 1; j <= n; j++) {
			if(i != j)
				dist[i][j] = 1e9;
		}
	}

	for(int i = 1; i <= n; i++) {
		wrong[i].reset();
	}
}

bool check(int n, int x, int y, int k) {
	vector<vector<pii>> sorto(n+1);
	for(int i = 1; i <= n; i++) {
		if(dist[i][x] <= dist[i][y]) {
			sorto[dist[i][x]].pb({i, 1});
		} else {
			sorto[dist[i][y]].pb({i, 2});
		}
	}
	
	vector<pii> A;
	vector<pii> B;
	for(int i = 0; i <= n; i++) {
		for(auto tmp : sorto[i]){
			if(tmp.second == 1) {
				A.pb({tmp.first, i});
			} else {
				B.pb({tmp.first, i});
			}
		}
	}

	//add bad from the same
	bitset<S> sameSet;
	bitset<S> tmp;
	for(auto v: A) {
		sameSet.set(v.st);
	}
	for(auto v: A) {
		tmp = wrong[v.st] & sameSet;
		if(tmp.any())
			return false;
	}
	sameSet.reset();
	for(auto v: B) {
		sameSet.set(v.st);
	}
	for(auto v: B) {
		tmp = wrong[v.st] & sameSet;
		if(tmp.any())
			return false;
	}

	//check from different sets
	int r = B.size();

	bitset<S> differentSet;
	for(int l = 0; l < A.size(); l++) {
		while(r > 0 && (A[l].nd + B[r-1].nd > k)) {
			r--;
			differentSet.set(B[r].st);
		}
		tmp = wrong[A[l].st] & differentSet;
		if(tmp.any())
			return false;
	}


	return true;
}

void solve() {
	int n;
	cin >> n;
	clear(n);
	for(int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		for(int j = 0; j < n; j++) {
			if(s[j] == '1')
				dist[i][j+1] = 1;
		}
	}

	for(int k = 1; k <= n; k++){
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}

	int l = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			l = max(l, dist[i][j]);
		}
	}

	vector<pii> V;
	for(int i = 1; i <= n; i++) {
		for(int j = i+1; j <= n; j++) {
			V.pb({i,j});
		}
	}
	for(int k = l-1; k >= 1; k--) {
		//first add bad edges for k+1
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				if(dist[i][j] == k+1) {
					wrong[i].set(j,1);
				}
			}
		}

		
		//now iterate over pairs
		bool found = false;
		while(!V.empty()) {
			auto [x,y] = V.back();
			if(!check(n,x,y,k)) {
				V.pop_back();
				continue;
			} else {
				// deb(x,y,k);
				found = true;
				break;
			}
		}
		if(!found) {
			cout << k+1 << "\n";
			return;
		}
	}
	cout << "1\n";
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	cout << fixed << setprecision(10);
	int t;
	cin >> t;
	while(t--) {
		solve();
	}
}