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
// Arti1990, II UWr

#include <bits/stdc++.h>

#define forr(i, n)                  for(int i=0; i<n; i++)
#define FOREACH(iter, coll)         for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll)        for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,K,FUN)             ({auto SSS=P, PPP = P-1, KKK=(K)+1; while(PPP+1!=KKK) {SSS = (PPP+(KKK-PPP)/2); if(FUN(SSS)) KKK = SSS; else PPP = SSS;} PPP;})
#define testy()                     int _tests; cin>>_tests; FOR(_test, 1, _tests)
#define CLEAR(tab)                  memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll)           (coll.find(el) != coll.end())
#define FOR(i, a, b)                for(int i=a; i<=b; i++)
#define FORD(i, a, b)               for(int i=a; i>=b; i--)
#define MP                          make_pair
#define PB                          push_back
#define ff                          first
#define ss                          second
#define deb(X)                      X;
#define SIZE(coll) 					((int)coll.size())

#define M 1000000007
#define INF 1000000007LL

using namespace std;

int n, m, a;
string str;
int d[1007][1007];

int check(int t1, int t2)
{
    int r = 0;
    forr(i, n)
        FOR(j, i+1, n-1)
            r = max(r, min(d[i][j], min(d[i][t1] + d[t2][j], d[i][t2] + d[t1][j])));
    return r;
}

int solve()
{
    cin >> n;
    forr(i, n)
    {
        cin >> str;
        forr(j, n)
            d[i][j] = i == j ? 0 : str[j] == '1' ? 1 : INF;
    }
    forr(k, n)
        forr(i, n)
            forr(j, n)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    
    /*forr(i, n)
    {
        forr(j, n)
            cout << d[i][j] << ' ';
        cout << '\n';
    }*/

    int res = 0;
    forr(i, n)
        FOR(j, i+1, n-1)
            res = max(res, d[i][j]);

    vector <pair<int, int>> candidates;

    forr(t1, n)
        FOR(t2, t1+1, n-1)
            candidates.PB({t1, t2});

    // trochę to nieeleganckie, ale cóż :/
    random_shuffle(candidates.begin(), candidates.end());

    int LIMIT = 11000;
    for(int i = 0; i < LIMIT && i < candidates.size(); i++)
        res = min(res, check(candidates[i].first, candidates[i].second));

    /*forr(t1, n)
        FOR(t2, t1+1, n-1)
            res = min(res, check(t1, t2));
    /*
    forr(t1, n)
    {
        int t2 = t1;
        forr(i, n)
            if(d[t1][i] > d[t1][t2])
                t2 = i;
        res = min(res, check(t1, t2));
    }
    */
    cout << res << '\n';

    return 0;
}

int main()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int x;
    srand(time(0) + (long long)&x);

    testy()
        solve();

    return 0;
}