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
/// UH Top
#include <bits/stdc++.h>
#define db(x)   cerr << #x << ':' << (x) << '\n';
#define all(v)  (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
// #define int     ll
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// typedef __int128_t int128;
typedef pair<ll, ll> pii;
typedef pair<ld, ll> pdi;
typedef pair<ld, ld> pdd;
typedef pair<ld, pdd> pdp;
typedef pair<string, ll> psi;
typedef pair<ll, string> pls;
typedef pair<string, string> pss;
typedef pair<ll, pii> pip;
typedef pair<pii, pii> ppp;
typedef complex<ld> point;
typedef vector<point> polygon;
typedef vector<ll> vi;
typedef pair<point, int> ppi;
#define prec(n)                                                                \
	cout.precision(n);                                                         \
	cout << fixed
const ll mod = (1e9 + 7);
const ld eps = (1e-9);
const ll oo = (ll)(1e9 + 5);
#define pi   acos(-1)
#define MAXN (ll)(4e2 + 5)

vector<vector<int>> g;
vector<int> color, mk;
vector<int> halves[2];

bool dfs(int u) {
	mk[u] = 1;
	if (g[u].size() > 1) {
		halves[color[u]].push_back(u);
	}
	for (auto v : g[u]) {
		if (!mk[v]) {
			color[v] = 1 - color[u];
			if (!dfs(v)) {
				return 0;
			}
		} else if (color[v] == color[u]) {
			return 0;
		}
	}
	return 1;
}

bool ok(int x, vector<vector<int>> &d) {
	int n = d.size();
	g = vector<vector<int>>(n);
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			if (d[i][j] > x) {
				g[i].push_back(j);
				g[j].push_back(i);
			}
		}
	}

	color = mk = vector<int>(n);
	halves[0].clear();
	halves[1].clear();

	int isBipartite = 1;
	for (int i = 0; i < n; i++)
		if (!mk[i]) {
			isBipartite &= dfs(i);
		}

	if (!isBipartite) {
		return 0;
	}

	if (halves[0].empty()) {
		return 1;
	}

	vector<int> best(2, n);
	for (int i = 0; i < 2; i++) {
		for (int u = 0; u < n; u++) {
			int furthest = 0;
			for (int v : halves[i]) {
				furthest = max(furthest, d[u][v]);
			}
			best[i] = min(best[i], furthest);
		}
	}

	return best[0] + best[1] <= x;
}

void solveTest() {
	int n;
	cin >> n;
	vector<string> raw(n);
	vector<vector<int>> d(n, vector<int>(n));
	for (int i = 0; i < n; i++) {
		cin >> raw[i];
		for (int j = 0; j < n; j++) {
			d[i][j] = raw[i][j] - '0';
			if (i != j && d[i][j] == 0) {
				d[i][j] = oo;
			}
		}
	}

	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				d[j][k] = min(d[j][i] + d[i][k], d[j][k]);

	int ans = -1;
	for (int i = (1 << 8); i; i >>= 1) {
		if (!ok(ans + i, d)) {
			ans += i;
		}
	}
	ans++;
	cout << ans << '\n';
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--) {
		solveTest();
	}

	return 0;
}