<Problem>
https://www.spoj.com/PTIT/problems/BCACM11E/
#include <iostream>
#include <queue>
using namespace std;
bool* visited = new bool[101]{ false };
void BFS(int arr[][101], int n, int index) {
queue <int> iDex;
iDex.push(index);
visited[index] = true;
while (!iDex.empty()) {
int index = iDex.front();
iDex.pop();
for (int i = 1; i <= n; i++) {
if (arr[index][i] == 1 && !visited[i]) {
visited[i] = true;
iDex.push(i);
}
}
}
}
int get(int arr[][101], int n) {
int num = 0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
num++;
BFS(arr, n, i);
}
}
return num;
}
void reset() {
for (int i = 1; i <= 101; i++) {
visited[i] = false;
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int arr[101][101];
int brr[101][101];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> arr[i][j];
//brr[i][j] = arr[i][j];
}
}
int maxNumOfConnection = get(arr, n);
//cout << maxNumOfConnection << endl;
int iDe;
int result = 0;
for (int i = 1; i <= n; i++) {
iDe = i;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (iDe == i || iDe == j) {
brr[i][j] = 0;
}
else brr[i][j] = arr[i][j];
}
}
reset();
visited[iDe] = true;
int numOfConnection = get(brr, n);
if (numOfConnection > maxNumOfConnection) {
maxNumOfConnection = numOfConnection;
result = i;
}
}
cout << result << endl;
reset();
}
return 0;
}