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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const double PI = acos(-1.0);
#define fst first
#define snd second
#define all(c) ((c).begin()), ((c).end())
#define pb push_back
template<class A,  class B>  auto&  operator<<(ostream &o, pair<A, B> p)  {
	return o <<  '('  << p.first <<  ", "  << p.second <<  ')';
}
template<class T>  auto  operator<<(ostream &o, T x)  ->  decltype(x.end(), o)  {
	o <<  '{';  int i =  0;  for(auto e : x) o <<  (", ")+2*!i++  << e;  return o <<  '}';
}
#ifdef DEBUG
#define debug(x...) cerr <<  "["  #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...)  {}
#endif


const int MAXN = 410;

bool G[MAXN][MAXN];
short dist[MAXN][MAXN];
short distord[MAXN][MAXN]; // distord[v] -> vertices ordered ascending by distance from v
short distidx[MAXN][MAXN]; // distidx[v][d] -> index of first vertex in distord[v] at distance >= d
short sufmax[MAXN][MAXN][MAXN]; // sufmax[v][t][i] -> maximum of dist(t, u) for u in distord[v][i .. n]

struct distord_cmp {
    int p;
    distord_cmp(int _p) : p(_p) {}
    bool operator() (short l, short r) const {
        return dist[p][l] < dist[p][r];
    }
};


void readG(int n) {
    // random shuffle for good measure
    vector<int> P(n);
    for (int i = 0; i < n; i++) {
        P[i] = i + 1;
    }
    random_shuffle(all(P));
    
    string s;
    for (int i = 0; i < n; i++) {
        cin >> s;
        for (int j = 0; j < n; j++) {
            int u = P[i]; int v = P[j];
            G[u][v] = s[j] == '1';
            G[v][u] = s[j] == '1';
        }
    }
}

void preprocess(int n) {
    // dist
    for (int u = 1; u <= n; u++) {
        for (int v = 1; v <= n; v++) {
            dist[u][v] = MAXN;
        }
        dist[u][u] = 0;
        queue<short> Q; Q.push(u);
        while (!Q.empty()) {
            int v = Q.front(); Q.pop();
            for (int w = 1; w <= n; w++) {
                if (G[v][w] && dist[u][w] > dist[u][v] + 1) {
                    dist[u][w] = dist[u][v] + 1;
                    Q.push(w);
                }
            }
        }
    }
    
    // distord and distidx
    for (int u = 1; u <= n; u++) {
        for (int v = 1; v <= n; v++) {
            distord[u][v] = v;
        }
        sort(distord[u] + 1, distord[u] + n + 1, distord_cmp(u));
        
        int d = 0;
        for (int i = 1; i <= n; i++) {
            while (d <= dist[u][distord[u][i]]) {
                distidx[u][d] = i;
                d++;
            }
        }
        while (d <= n + 1) {
            distidx[u][d] = n + 1;
            d++;
        }
    }
    
    // sufmax
    for (int v = 1; v <= n; v++) {
        for (int t = 1; t <= n; t++) {
            for (int i = n; i >= 1; i--) {
                sufmax[v][t][i] = max(sufmax[v][t][i + 1], dist[t][distord[v][i]]);
            }
        }
    }
}

bool check(int n, int m) {
    if (m > n) return true;
    for (int s = 1; s <= n; s++) {
        for (int t = s + 1; t <= n; t++) {
            bool legit = true;
            // teleport between s and t
            for (int v = 1; v <= n; v++) {
                // furthest distance from v
                int idx = distidx[v][m + 1];
                if (idx > n) continue;
                int c = dist[v][s] < dist[v][t] ? s : t; // closer to v
                int f = s + t - c; // further from v
                if (sufmax[v][f][idx] + dist[v][c] > m) {
                    legit = false;
                    break;
                }
            }
            if (legit) {
                return true;
            }
        }
    }
    return false;
}

void test() {
    int n; cin >> n;
    readG(n);
    preprocess(n);
    int l = 0;
    int r = n + 1;
    while (l < r) {
        int m = (l + r) / 2;
        if (!check(n, m)) {
            l = m + 1;
        } else {
            r = m;
        }
    }
    cout << l << "\n";
}

int main() {
    cin.tie(0); iostream::sync_with_stdio(0);
    int t; cin >> t;
    while (t--) test();
}