import java.io.FileInputStream;
import java.util.Scanner;
public class MarbleGame {
static int Answer = 0;
static int tempAnswer = 0;
static int[][] dir = { { 2, 4, 1, 3 }, { 4, 1, 2, 3 }, { 3, 1, 4, 2 }, { 2, 3, 4, 1 }, { 2, 1, 4, 3 } };
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
sc = new Scanner(new FileInputStream("src/input.txt"));
int T = sc.nextInt();
for (int test_case = 0; test_case < T; test_case++) {
int N = sc.nextInt();
int table[][] = new int[N + 1][N + 1];
int table2[][] = new int[N + 1][N + 1];
for (int ni = 1; ni < N + 1; ni++) {
for (int nj = 1; nj < N + 1; nj++) {
if (sc.hasNext())
table[ni][nj] = sc.nextInt();
}
}
if (N == 1)
Answer = table[1][1] % 10;
else {
for (int i = 1; i < N + 1; i++) {
for (int j = 1; j < N + 1; j++) {
if (table[i][j] == 0) {
for (int idir = 1; idir <= 4; idir++) {
for (int i2 = 1; i2 < N + 1; i2++) {
for (int j2 = 1; j2 < N + 1; j2++) {
table2[i2][j2] = table[i2][j2];
}
}
tempAnswer = FindMaxScore(N, table2, i, j, idir);
if (tempAnswer > Answer)
Answer = tempAnswer;
tempAnswer = 0;
}
}
}
}
}
System.out.println("Case #" + (test_case + 1));
System.out.println(Answer);
Answer = 0;
}
}
private static int FindMaxScore(int n, int[][] table, int i, int j, int idir) {
int[][] temptable = new int[n + 1][n + 1];
temptable = table;
int tempx;
int tempy;
int type;
int health;
switch (idir) {
case 1:
for (int ix = 1; ix < n + 1; ix++) {
if (temptable[ix][j] != 0)
ix = n + 1;
else if (ix == n)
return tempAnswer;
}
if ((i - 1) == 0)
tempx = n;
else
tempx = i - 1;
if (temptable[tempx][j] == 0)
FindMaxScore(n, temptable, tempx, j, idir);
else {
health = temptable[tempx][j] % 10;
type = (temptable[tempx][j] - health) / 10;
tempAnswer++;
health--;
idir = dir[type - 1][idir - 1];
if (health == 0)
temptable[tempx][j] = 0;
else
temptable[tempx][j]--;
FindMaxScore(n, temptable, tempx, j, idir);
}
break;
case 2:
for (int ix = 1; ix < n + 1; ix++) {
if (temptable[ix][j] != 0)
ix = n + 1;
else if (ix == n)
return tempAnswer;
}
if (i == n)
tempx = 1;
else
tempx = i + 1;
if (temptable[tempx][j] == 0)
FindMaxScore(n, temptable, tempx, j, idir);
else {
health = temptable[tempx][j] % 10;
type = (temptable[tempx][j] - health) / 10;
tempAnswer++;
health--;
idir = dir[type - 1][idir - 1];
if (health == 0)
temptable[tempx][j] = 0;
else
temptable[tempx][j]--;
FindMaxScore(n, temptable, tempx, j, idir);
}
break;
case 3:
for (int jy = 1; jy < n + 1; jy++) {
if (temptable[i][jy] != 0)
jy = n + 1;
else if (jy == n)
return tempAnswer;
}
if (j - 1 == 0)
tempy = n;
else
tempy = j - 1;
if (temptable[i][tempy] == 0)
FindMaxScore(n, temptable, i, tempy, idir);
else {
health = temptable[i][tempy] % 10;
type = (temptable[i][tempy] - health) / 10;
tempAnswer++;
health--;
idir = dir[type - 1][idir - 1];
if (health == 0)
temptable[i][tempy] = 0;
else
temptable[i][tempy]--;
FindMaxScore(n, temptable, i, tempy, idir);
}
break;
case 4:
for (int jy = 1; jy < n + 1; jy++) {
if (temptable[i][jy] != 0)
jy = n + 1;
else if (jy == n)
return tempAnswer;
}
if (j == n)
tempy = 1;
else
tempy = j + 1;
if (temptable[i][tempy] == 0)
FindMaxScore(n, temptable, i, tempy, idir);
else {
health = temptable[i][tempy] % 10;
type = (temptable[i][tempy] - health) / 10;
tempAnswer++;
health--;
idir = dir[type - 1][idir - 1];
if (health == 0)
temptable[i][tempy] = 0;
else
temptable[i][tempy]--;
FindMaxScore(n, temptable, i, tempy, idir);
}
break;
}
return tempAnswer;
}
}
sample input:
2
5
0 0 31 0 0
51 31 0 0 0
0 0 42 31 32
0 0 21 0 0
0 11 0 0 32
6
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 11 0 0 21
52 0 0 0 22 0
0 0 0 0 0 0