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
#include <cstdio>
#include <climits>

#define MAXN 400
#define ALOT MAXN*MAXN*2
#define DEBUG 0

int n;
int Original[MAXN][MAXN];
int Nei[MAXN][MAXN]; /* -1 terminated */
int Nei_count[MAXN];
int Dist[MAXN][MAXN];
int Prev[MAXN][MAXN];
int queue[MAXN];
int visited[MAXN];
int queue_head, queue_tail;

void reset_queue() {
    queue_head = 0;
    queue_tail = 0; /* poinitng after the last element */
}

#define PUSH(x) queue[queue_tail++] = x
#define POP() queue[queue_head++]
#define EMPTY() (queue_head == queue_tail)

void dump_dist(int n)
{
    if (!DEBUG)
        return;

    printf("=================\n");
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            printf("%3d ", Dist[i][j]);
        printf("\n");
    }
    printf("=================\n\n");
}

/* Floyd-warshall algorithm */
void FW(int M[MAXN][MAXN], int n)
{
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) {
            Prev[i][j] = -1;

            if (i == j) {
                Dist[i][j] = 0;
            } else if (M[i][j]) {
                Dist[i][j] = 1;
                Prev[i][j] = i;
            } else
                Dist[i][j] = ALOT;

        }

    for (int k = 0; k < n; ++k)
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                if (Dist[i][j] > Dist[i][k] + Dist[k][j]) {
                    Dist[i][j] = Dist[i][k] + Dist[k][j];
                    Prev[i][j] = Prev[k][j];
                }
}

int double_bfs(int a, int b)
{
    int x;
    reset_queue();
    PUSH(a);
    PUSH(b);

    for (int i = 0; i < n; ++i) {
        visited[i] = -1;
    }

    visited[a] = 0;
    visited[b] = 0;

    while (!EMPTY()) {
        x = POP();
        for (int i = 0; i < Nei_count[x]; ++i) {
            int y = Nei[x][i];
            if (visited[y] >= 0)
                continue;

            PUSH(y);
            visited[y] = visited[x] + 1;
        }
    }

    return visited[x];
}

int teleport()
{
    char input[MAXN];
    scanf("%d", &n);

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            Original[i][j] = 0;
            Nei[i][j] = -1;
        }

        Nei_count[i] = 0;
    }


    for (int i = 0; i < n; ++i) {
        scanf("%s", input);
        for (int j = 0; j < n; ++j) {
            if (input[j] == '1') {
                Original[i][j] = 1;
                Nei[i][Nei_count[i]++] = j;
            }
        }
    }

    FW(Original, n);
    dump_dist(n);

    /* find the optimal teleport */
    int worst_i = 0, worst_j = 0;
    int best = ALOT;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) {
            if (i == j)
                continue;
            int res = double_bfs(i, j);
            #if DEBUG 
                printf("%d-%d: worst dist is %d\n", i, j, res);
            #endif
            if (res < best) {
                best = res;
                worst_i = i;
                worst_j = j;
            }
        }

    /* upgrade the network */
    for (int i = 0; i < n; ++i) {
        if (Original[i][worst_j]) {
            Original[i][worst_i] = Original[worst_i][i] = 1;
        }

        if (Original[i][worst_i]) {
            Original[i][worst_j] = Original[worst_j][i] = 1;        
        }
    }

    if (DEBUG) printf("Connecting %d and %d\n", worst_i, worst_j);

    /* re-calculate the distances */
    FW(Original, n);
    dump_dist(n);

    int worst_dist = 0;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
            if (worst_dist < Dist[i][j])
                worst_dist = Dist[i][j];

    return worst_dist;
}

int main()
{
    int T;
    scanf("%d", &T);

    for (int t = 0; t < T; ++t) {
        printf("%d\n", teleport());
    }
}