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
#include <algorithm>
#include <cassert>
#include <iostream>
#include <iterator>
#include <optional>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;

namespace {

constexpr bool DEBUG = false;

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
// vector<vector<bool>> allConnections;
vector<vector<int>> neighbors;
vector<int> sean;
// set<int> furthers;
queue<std::pair<int, int>> bfsQ;
int currentLevel;
int n = 0;
int first = 0, second = 0;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

auto visitNext() {
    auto [indx, lvl] = bfsQ.front();
    if (indx == first && sean[second] == -1) {
        bfsQ.front().first = second;
        sean[second] = sean[first];
    } else if (indx == second && sean[first] == -1) {
        bfsQ.front().first = first;
        sean[first] = sean[second];
    } else {
        bfsQ.pop();
    }

    if (lvl > currentLevel) { // NOLINT
        currentLevel = lvl;
        // furthers.clear();
    }

    if constexpr (DEBUG) {
        cout << indx << ":" << currentLevel << " ";
    }

    // furthers.insert(indx);

    for (auto neighbour : neighbors[indx]) {
        if (sean[neighbour] != -1) {
            continue;
        }
        sean[neighbour] = indx;
        bfsQ.emplace(neighbour, currentLevel + 1);
        if (neighbour == first && sean[second] == -1) {
            bfsQ.emplace(second, currentLevel + 1);
            sean[second] = sean[first];
        } else if (neighbour == second && sean[first] == -1) {
            bfsQ.emplace(first, currentLevel + 1);
            sean[first] = sean[second];
        }
    }
}

auto createMap() {
    cin >> n;
    char tmpChar = 0;

    // allConnections.clear();
    // allConnections.reserve(n);
    neighbors.clear();
    neighbors.reserve(n);

    for (int i = 0; i < n; i++) {
        // allConnections.emplace_back();
        neighbors.emplace_back();
        // allConnections[i].assign(n, false);
        for (int j = 0; j < n; j++) {
            cin >> tmpChar;
            if (tmpChar == '1') {
                // allConnections[i][j] = true;
                neighbors[i].push_back(j);
            }
        }
    }
}

auto trip(int startIndx) -> void {
    sean.clear();
    sean.assign(n, -1);
    sean[startIndx] = startIndx;
    currentLevel = -1;

    assert(bfsQ.empty());
    bfsQ.emplace(startIndx, 0);

    while (!bfsQ.empty()) {
        visitNext();
    }

    if constexpr (DEBUG) {
        cout << "\n";
    }
}

} // namespace

auto main() -> int {
    std::ios_base::sync_with_stdio(false);
    int t = 0; // NOLINT
    cin >> t;
    int result = 0;
    for (int i = 0; i < t; i++) {
        createMap();
        result = n + 1;
        for (int a = 0; a < n; a++) {
            first = a;
            for (int b = a + 1; b < n; b++) {
                second = b;
                int maxLvl = 0;
                for (int c = 0; c < n; c++) {
                    trip(c);
                    maxLvl = std::max(maxLvl, currentLevel);
                }
                result = std::min(maxLvl, result);
            }
        }
        std::cout << result << "\n";
    }
}