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
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define JOIN_(X, Y) X##Y
#define JOIN(X, Y) JOIN_(X, Y)
#define TMP JOIN(tmp, __LINE__)
#define PB push_back
#define SZ(x) int((x).size())
#define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i)
#define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i)
#define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i)

#ifdef DEBUG
#define DEB(x) (cerr << x)
#else
#define DEB(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef unsigned int uint;

const int INF = 1e9 + 9;

ostream &operator<<(ostream &os, const vi &vec) {
    os << "[";
    for (int i = 0; i < SZ(vec); ++i) {
        os << vec[i];
        if (i != SZ(vec) - 1) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

struct LongestPath {
    int start, end, dist;
};

ostream &operator<<(ostream &os, const LongestPath &path) {
    os << "LongestPath(Start: " << path.start << ", End: " << path.end
       << ", Distance: " << path.dist << ")";
    return os;
}

int n;
vector<vector<bool>> b;
vector<vector<int>> dist;

LongestPath floyd_warshall_diameter() {
    vector<vector<int>> d = dist;
    FOR(k, 1, n) {
        FOR(i, 1, n) {
            FOR(j, 1, n) { d[i][j] = min(d[i][j], d[i][k] + d[k][j]); }
        }
    }
    int diameter = -1;
    int max_i = -1;
    int max_j = -1;
    FOR(i, 1, n) {
        FOR(j, 1, n) {
            if (d[i][j] > diameter) {
                diameter = d[i][j];
                max_i = i;
                max_j = j;
            }
        }
    }
    return {.start = max_i, .end = max_j, .dist = diameter};
}

vi build_shortest_path(int start, int end) {
    vi result;
    queue<int> q;
    vector<int> prev(n + 1, -1);
    prev[start] = -2;
    q.push(start);
    while (not q.empty()) {
        int x = q.front();
        q.pop();
        FOR(y, 1, n) {
            if ((x != y) and b[x][y] and prev[y] == -1) {
                prev[y] = x;
                // DEB("        push y=" << y << " with dist=" << dist[y] << "\n");
                q.push(y);
            }
        }
    }
    for (int x = end; x >= 0; x = prev[x]) {
        result.PB(x);
    }
    return result;
}

void inline one() {
    cin >> n;
    b = vector<vector<bool>>(n + 1, vector<bool>(n + 1, false));
    dist = vector<vector<int>>(n + 1, vector<int>(n + 1, INF));
    FOR(i, 1, n) {
        string line;
        cin >> line;
        FOR(j, 1, n) {
            b[i][j] = (line[j - 1] == '1');
            if (i == j) {
                dist[i][j] = 0;
            } else {
                dist[i][j] = ((line[j - 1] == '1') ? 1 : INF);
            }
        }
    }
    LongestPath best_1 = floyd_warshall_diameter();
    DEB("best_1=" << best_1 << "\n");
    vi path_1 = build_shortest_path(best_1.start, best_1.end);
    DEB("path_1=" << path_1 << "\n");

    int index_1 = ceil(static_cast<double>(SZ(path_1) - 1) * 1.0 / 4.0);
    int index_2 = floor(static_cast<double>(SZ(path_1) - 1) * 3.0 / 4.0);
    DEB("indexes=" << index_1 << ", " << index_2 << "\n");

    vector<pii> indexes;
    indexes.emplace_back(0, SZ(path_1) - 1);
    indexes.emplace_back(index_1, index_2);
    if (index_1 - 1 >= 0) {
        indexes.emplace_back(index_1 - 1, index_2);
    }

    if (index_2 + 1 < SZ(path_1)) {
        indexes.emplace_back(index_1, index_2 + 1);
    }

    if ((index_1 - 1 >= 0) and (index_2 + 1 < SZ(path_1))) {
        indexes.emplace_back(index_1 - 1, index_2 + 1);
    }

    vector<vector<int>> dist_backup = dist;

    int result = best_1.dist;
    for (const auto &[i_1, i_2] : indexes) {
        int node_1 = path_1[i_1];
        int node_2 = path_1[i_2];

        DEB("ixs=" << i_1 << ", " << i_2 << "\n");
        DEB("nodes=" << node_1 << ", " << node_2 << "\n");

        dist[node_1][node_2] = dist[node_2][node_1] = 0;
        LongestPath best_after_teleport = floyd_warshall_diameter();
        DEB("best_after_teleport=" << best_after_teleport << "\n");
        result = min(result, best_after_teleport.dist);
        dist = dist_backup;
    }

    cout << result << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int z;
    cin >> z;
    while (z--)
        one();
}