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
#include <iostream>
#include <vector>
#include <queue>
#include <utility>

using namespace std;

const int MAX_N = 404;

int t, n;
string s;

int removeVertexB(int x, int A, int B) {
	if (x == B)
		return A;
	return x;
}

vector<int> longest_path_vector;
pair<int, int> BFS(vector<vector<int>> V, int x, int teleportA=-1, int teleportB=-1) {
	//cout << "BFS(V, " << x << ", " << teleportA << ", " << teleportB << ")" << endl;
	queue<int> Q;
	const int size = V.size();
	int distance[MAX_N] = {};
	int parent[MAX_N] = {};
	for (int i = 0; i < V.size(); ++i) {
		distance[i] = -1;
		parent[i] = -1;
	}

	x = removeVertexB(x, teleportA, teleportB);
	Q.push(x);
	distance[x] = 0;
	parent[x] = x;


	int max_distance = 0;
	int max_vertex = x;
	
	while (!Q.empty()) {
		int v = Q.front();
		Q.pop();
		for (int i = 0; i < V[v].size(); ++i) {
			int u = removeVertexB(V[v][i], teleportA, teleportB);
			//cout << "\t" << v << "_" << u << endl;
			if (distance[u] != -1)
				continue;
			
			Q.push(u);
			distance[u] = distance[v] + 1;
			parent[u] = v;
			if (distance[u] >= max_distance) {
				max_vertex = u;
				max_distance = distance[u];
			}
		}
		if (v == teleportA) {
			for (int i = 0; i < V[teleportB].size(); ++i) {
				int u = removeVertexB(V[teleportB][i], teleportA, teleportB);
				//cout << "\t" << v << "_" << u << endl;
				if (distance[u] != -1)
					continue;

				Q.push(u);
				distance[u] = distance[v] + 1;
				parent[u] = v;
				if (distance[u] >= max_distance) {
					max_vertex = u;
					max_distance = distance[u];
				}
			}
		}
	}

	/*for (int i = 0; i < V.size(); ++i) {
		cout << i << ":\t" << distance[i] << "\t" << parent[i] << endl;
	}*/

	int p = max_vertex;
	longest_path_vector.clear();
	longest_path_vector.push_back(p);
	while (p != x) {
		p = parent[p];
		longest_path_vector.push_back(p);
	}

	return make_pair(max_distance, max_vertex);
}

int find_longest_path(vector<vector<int>> V, int teleportA=-1, int teleportB=-1) {
	pair<int, int> result = BFS(V, 0, teleportA, teleportB);
	pair<int, int> longest_result = BFS(V, result.second, teleportA, teleportB);
	return longest_result.first;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> t;
	for (int tt = 0; tt < t; ++tt) {
		cin >> n;
		vector<vector<int>> V;
		for (int i = 0; i < n; ++i) {
			cin >> s;
			vector<int> tmp;
			for (int j = 0; j < n; ++j) {
				if (s[j] == '1')
					tmp.push_back(j);
			}
			V.push_back(tmp);
		}

		pair<int, int> result = BFS(V, 0);
		pair<int, int> longest_result = BFS(V, result.second);

		/*for (int i = 0; i < longest_path_vector.size(); ++i) {
			cout << longest_path_vector[i] << " ";
			if (i % 10 == 9)
				cout << endl;
		}
		cout << endl;
		cout << "dupa" << endl;*/
		vector<int> path = longest_path_vector;
		int min_path_length = 2 * n;
		for (int a = -5; a < 5; ++a) {
			for (int i = 0; i < path.size(); ++i) {
				int j = i + path.size() / 2 + a;
				if (j < 0 || j == i)
					continue;
				if (j >= path.size())
					break;
				int max_path_length = find_longest_path(V, path[i], path[j]);
				if (max_path_length < min_path_length)
					min_path_length = max_path_length;
			}
		}
		

		cout << min_path_length << endl;
		/*int min_path_length = 2 * n;
		for (int i = 0; i < n; ++i) {
			for (int j = i + 1; j < n; ++j) {
				int max_path_length = find_longest_path(V, i, j);
				if (max_path_length < min_path_length)
					min_path_length = max_path_length;
			}
		}
		cout << min_path_length << endl;*/


		/*cout << endl;
		for (int i = 0; i < V.size(); ++i) {
			cout << i << ":\t";
			for (int j = 0; j < V[i].size(); ++j) {
				cout << V[i][j].first << ", ";
			}
			cout << endl;
		}*/
	}
}