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
#include <algorithm>
#include <bitset>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) (int)(x).size()
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define REP(x, n) for(int x = 0; x < (n); x++)
#define FOR(x, b, e) for(int x = (b); x <= (e); x++)
#define FORD(x, b, e) for(int x = (b); x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)

typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

const int MXN = 410;
const int C = 262144;
const int INF = 1000000001;
const int MOD = 1000000007;
const int B = 401;

int n;
bitset<B> X[MXN][MXN];
int dist[MXN][MXN];

void bfs(int x) {
    queue<int> Q;
    Q.push(x);
    FOR(i, 1, n)
        dist[x][i] = -1;
    dist[x][x] = 0;

    while(!Q.empty() ) {
        int w = Q.front();
        Q.pop();
        FOR(i, 1, n)
            if(X[1][w][i] && dist[x][i] == -1) {
                dist[x][i] = dist[x][w] + 1;
                Q.push(i);
            }
    }

    FOR(i, 1, n)
        X[dist[x][i]][x][i] = 1;
    FOR(i, 1, n)
        X[i][x] = X[i - 1][x] | X[i][x];
}

bool check(int k) {
    FOR(x, 1, n)
        FOR(y, x + 1, n) {
            bool dec = 1;
            FOR(v, 1, n) {
                int a = x;
                int b = y;
                if(dist[v][a] > dist[v][b])
                    swap(a, b);
                if(dist[v][a] > k) {
                    dec = 0;
                    break;
                }
                bitset<B> total = X[k][v] | X[k - dist[v][a]][b];
                if(total.count() < n) {
                    dec = 0;
                    break;
                }
            }
            if(dec)
                return 1;
        }
    return 0;
}

void test() {
    scanf("%d", &n);
    
    FOR(i, 1, n)
        FOR(j, 1, n)
            X[i][j].reset();

    FOR(i, 1, n) {
        char zn;
        FOR(j, 1, n) {
            while(1) {
                zn = getchar();
                if(!isdigit(zn))
                    continue;
                X[1][i][j] = zn - '0';
                break;
            }
        }
    }

    FOR(i, 1, n)
        bfs(i);

    int res = n - 1;
    int L = 1, R = n - 1;
    while(L <= R) {
        int S = (L + R) / 2;
        if(check(S) ) {
            res = min(res, S);
            R = S - 1;
        }
        else {
            L = S + 1;
        }
    }
    printf("%d\n", res);
}

int main() {
	int te = 1;
	scanf("%d", &te);
	while(te--)
		test();
	return 0;
}