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
179
180
181
182
183
184
185
186
187
188
#include<bits/stdc++.h>

int main(){
	using namespace std;
	ios::sync_with_stdio(false), cin.tie(nullptr);
	
	//~ freopen("tel2.in", "r", stdin);
	
	int tt;
	cin >> tt;
	
	while(tt--){
		int n;
		cin >> n;
		
		const int inf = (int)1e9;
		vector<vector<int>> d(n, vector<int> (n, inf));
		
		for(int a = 0;a < n;a++){
			for(int b = 0;b < n;b++){
				char c;
				cin >> c;
				if(c == '1') d[a][b] = 1;
			}
			d[a][a] = 0;
		}
		
		if(n <= 2){
			cout << 0 << '\n';
			continue;
		}
		
		// grupy[i] -> jak będą wygłądał graf po usunięciu i.
		vector<vector<vector<int>>> grupy(n); 
		
		for(int c = 0;c < n;c++){
			for(int a = 0;a < n;a++){
				for(int b = 0;b < n;b++){
					d[a][b] = min(d[a][b], d[a][c] + d[c][b]);
				}
			}
		}
				
		for(int a = 0;a < n;a++){
			// usuwamy a
			vector<int> vis(n);
			vis[a] = 1;
			
			for(int b = 0;b < n;b++){
				if(vis[b]) continue;
				//~ cerr << "b: " << b << endl;
				vector<int> v;
				queue<int> que;
				vis[b] = 1;
				que.push(b);
				while(!que.empty()){
					int x = que.front();
					que.pop();
					v.push_back(x);
					for(int i = 0;i < n;i++){
						if(!vis[i] && d[x][i] == 1){
							vis[i] = 1;
							que.push(i);
						}
					}
				}
				int max_dist = 0;
				for(int _a : v)
					for(int _b : v)	
						max_dist = max(max_dist, d[_a][_b]);
				v.push_back(max_dist);
				grupy[a].push_back(v);
			}
		}
		
		//~ for(int a = 0;a < n;a++){
			//~ cerr << "a: " << a << endl;
			//~ for(const vector<int> &g : grupy[a]){
				//~ cerr << " { ";
				//~ for(int x : g) cerr << x << ' ';
				//~ cerr << " } ";
			//~ }
			//~ cerr << endl;
		//~ }
		

		int ans = inf;
		
		{ // coś co może ma sens i jakoś na to wpadłem
			for(int a = 0;a < n;a++){
				for(int b = a + 1;b < n;b++){
					// (a, b)
					// there is also a case where furthest pair is inside some group.
					// how to find this one ?
					int cur_ans = 0;
					vector<vector<int>> common_groups;
					pair<int, int> max_alone = {0, 0};
					auto upd = [&](int x)->void{
						if(x >= max_alone.first){
							max_alone.second = max_alone.first;
							max_alone.first = x;
							return ;
						}
						if(x >= max_alone.second)
							max_alone.second = x;
					};
					for(vector<int> &g : grupy[a]){
						int Z = g.back(); g.pop_back();
						bool found_b = false;
						int max_dist = 0;
						for(int x : g){
							max_dist = max(max_dist, d[a][x]);
							if(x == b) found_b = true;
						}
						if(found_b) common_groups.push_back(g);
						else {
							upd(max_dist);
							cur_ans = max(cur_ans, Z);
						}
						g.push_back(Z);
					}
					for(vector<int> &g : grupy[b]){
						int max_dist = 0;
						int Z = g.back(); g.pop_back();
						bool found_a = false;
						for(int x : g){
							if(x == a){
								found_a = true;
								break;
							}
							max_dist = max(max_dist, d[x][b]);
						}
						if(!found_a){
							upd(max_dist);
							cur_ans = max(cur_ans, Z);
						}
						g.push_back(Z);
					}
					int max_common = 0;
					//~ assert((int)common_groups.size() == 1);
					int who = -1;
					for(const vector<int> &g : common_groups){
						int max_dist = 0;
						for(int x : g){
							if(min(d[x][a], d[x][b]) > max_dist){
								who = x;
							}
							max_dist = max(max_dist, min(d[x][a], d[x][b]));
						}
						max_common = max(max_common, max_dist);
					}
					
					cur_ans = max(cur_ans, max_alone.first + max_alone.second);
					cur_ans = max(cur_ans, max_alone.first + max_common); 
					
					if(who != -1){
						const vector<int> &g = common_groups.back();
						for(int x : g){
							cur_ans = max(cur_ans, d[x][who]);
						}
					}
					
					ans = min(ans, cur_ans);	
				}
			}
		}
		
		
		cout << ans << '\n';
	}
	
	return 0;
}

/*
2
4
0111
1011
1101
1110
5
01000
10100
01010
00101
00010
*/