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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

vi graph[400];
int dist[400][400];
bool odw[400];
int n;

void bfs(int v)
{
    queue<pii> q;
    q.push({v,0});
    rep(i,n) odw[i] = 0;
    while(!q.empty())
    {
        pii t = q.front();
        q.pop();
        if(odw[t.ff]) continue;
        odw[t.ff] = 1;
        dist[v][t.ff] = t.ss;
        forall(it,graph[t.ff])
        {
            q.push({it,t.ss+1});
        }
    }
}

int ansPair[400][400];
int pref_max[403];
int suf_max[403];
int buc_sort[403][400];
int buc_cnt[403];
vector<pii> sort_v[400];

void solve()
{
    cin >> n;
    rep(i,n) graph[i] = {};
    rep(i,n)
    {
        rep(j,n)
        {
            char z;
            cin >> z;
            if(z == '1') graph[i].pb(j);
        }
    }
    rep(i,n) bfs(i);
    rep(i,n) sort_v[i] = {};
    rep(i,n) rep(j,n) sort_v[i].pb({dist[i][j],j});
    rep(i,n) sort(all(sort_v[i]));
    int ans = 1e9;
    int p;
    rep(t1,n)
    {
        //if(t1 != 0) continue;
        rep(t2,n) ansPair[t1][t2] = 0;
        rep(p1,n)
        {
            rep(i,n+3)
            {
                buc_cnt[i] = 0;
            }
            rep(p2,n)
            {
                p = max(0,dist[p1][p2] - dist[t1][p2] + 1);
                buc_sort[p][buc_cnt[p]++] = p2; 
            }
            int cur_pref = -1;
            rep(i,n+3)
            {
                rep(j,buc_cnt[i])
                {
                    int p2 = buc_sort[i][j];
                    while(cur_pref < n && dist[t1][p2] + sort_v[p1][cur_pref+1].ff <= dist[p1][p2])
                    {
                    //    cout << dist[t1][p2] + sort_v[p1][cur_pref+1].ff << " d2\n";
                        cur_pref++;
                    }
                    if(cur_pref != -1)
                    {
                        pref_max[cur_pref] = max(pref_max[cur_pref],dist[p2][t1]);
                    }
                    suf_max[cur_pref+1] = max(suf_max[cur_pref+1],dist[p1][p2]);
               //     cout << p1 << " " << p2 << " " << cur_pref << " " << dist[p2][t1] << " pref\n";
             //       if(cur_pref != -1) cout << sort_v[p1][cur_pref].ss << " " << sort_v[p1][cur_pref+1].ss << " " << sort_v[p1][cur_pref+2].ss << " sort_v\n";
                }
            }
            int cur_max = 0;
            rep(i,n+3)
            {
                cur_max = max(cur_max,suf_max[i]);
                suf_max[i] = cur_max;
            //    cout << suf_max[i] << " suf\n";
            }
            cur_max = 0;
            for(int i = n+2; i >= 0; i--)
            {
                cur_max = max(cur_max,pref_max[i]);
                pref_max[i] = cur_max;
            }
            rep(i,n)
            {
                if(dist[p1][sort_v[p1][i].ss] <= dist[p1][t1])
                {
            //        cout << "i: " << i << " P: " << p1 << " t: " << t1 << " " << sort_v[p1][i].ss << " val: " << suf_max[i] << " " << sort_v[p1][i].ff << " " << pref_max[i] << " \n";
                    ansPair[t1][sort_v[p1][i].ss] = max(ansPair[t1][sort_v[p1][i].ss],max(suf_max[i],sort_v[p1][i].ff + pref_max[i]));
                }
                pref_max[i] = 0;
                suf_max[i] = 0;
            }
        }
       // rep(i,n) ans = min(ans,ansPair[t1][i]);
       // rep(i,n)
        //{
        //    cout << t1 << " " << i << " " << ansPair[t1][i] << " ansPair\n";
        //}
    }
   // rep(i,n)
   // {
   //     rep(j,n)
   //     {
   //         cout << ansPair[i][j] << " ";
   //     }
   //     cout << "\n";
   // }
   // cout << " ans\n";
    rep(i,n)
    {
        rep(j,n)
        {
            ans = min(ans,max(ansPair[i][j],ansPair[j][i]));
        }
    }
    cout << ans << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random_start();
    int t = 1;
    cin >> t;
    while(t--) solve();
}