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
#include <bits/stdc++.h>
using namespace std;

#define INF 1000

int d[407][407];
pair<int, pair<int, int>> pairs[160007];

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

    // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    int t;
    cin >> t;
    while (t > 0) {
        t--;

        int n;
        cin >> n;

        // vector<int> permutation(n);
        // for (int i = 0; i < n; i++)
        //     permutation[i] = i;

        // shuffle(permutation.begin(), permutation.end(), rng);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                char c;
                cin >> c;
                if (i == j) {
                    d[i][j] = 0;
                }
                else if (c == '1') {
                    d[i][j] = 1;
                }
                else {
                    d[i][j] = INF;
                }
            }
        }

        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]); 
                }
            }
        }

        int id = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                pairs[id] = {d[i][j], {i, j}};
                id++;
            }
        }

        sort(pairs, pairs + id);
        int k = pairs[id - 1].first;

        // for (int i = 0; i < n; i++) { // pierwszy teleport
        //     for (int j = i + 1; j < n; j++) { // drugi teleport

        //         int a = permutation[i], b = permutation[j];

        for (int a = 0; a < n; a++) { // pierwszy teleport
            for (int b = a + 1; b < n; b++) { // drugi teleport
                int maks = 0; // najdłuższy shortcut
                int last = id - 1; // ostatni nieskrócony

                while (last >= 0){
                    int x = pairs[last].second.first;
                    int y = pairs[last].second.second;

                    if (maks >= d[x][y]) {
                        break;
                    }

                    int shortcut = min(d[a][x] + d[b][y], d[a][y] + d[b][x]);
                    if (shortcut >= d[x][y]) {
                        break;
                    }
                    maks = max(shortcut, maks);
                    last--;
                }

                if (last > 0) {
                    k = min(k, max(pairs[last].first, maks));
                }
                else {
                    k = min(k, maks);
                }
            }
        }

        cout << k << "\n";
    }
}