#include <iostream>
#include <vector>
using namespace std;
int N;
int input_array[20][20];
int dessert[101] = { 0, };
int dArr[][2] = { { -1,1 },{ 1,1 },{ 1,-1 },{ -1,-1 } };
int first_x, first_y;
int max_count = -1;
void recursive(int a, int b, int n, int direction) {
int x = a + dArr[direction][0];
int y = b + dArr[direction][1];
if (x >= N || y >= N || x < 0 || y < 0 || direction > 3) // ๋ฐฐ์ด ๋ฒ์๋ฅผ ๋๊ฑฐ๋ 4๋ฐฉํฅ ํ์ ์ ๋์์ ๊ฒฝ์ฐ
return;
if (dessert[input_array[x][y]] == 1) // ๊ฐ์ ์ข
๋ฅ์ ๋์ ํธ๋ฅผ ํ๊ณ ์์ ๊ฒฝ์ฐ
return;
if (direction == 3 && first_x == x && first_y == y){ // 4๋ฐฉํฅ ํ์ ์ ํ๊ณ ์์ ์ผ๋ก ๋์์จ ๊ฒฝ์ฐ
if (n > max_count)
max_count = n;
}
dessert[input_array[x][y]] = 1;
recursive(x, y, n + 1, direction); // ๊ธฐ์กด ๋ฐฉํฅ์ผ๋ก
recursive(x, y, n + 1, direction+1); // ๋ค์ ๋ฐฉํฅ์ผ๋ก
dessert[input_array[x][y]] = 0;
return;
}
int main(int argc, char** argv)
{
int test_case;
int T;
cin >> T;
for (test_case = 1; test_case <= T; ++test_case)
{
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> input_array[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
first_x = i;
first_y = j;
recursive(i, j, 1, 0);
}
}
cout << "#" << test_case << " " << max_count << endl;
max_count = -1;
}
return 0;
}