package work;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class APlaneMy {
static int N;
static int x, y;
static int[][] data;
static boolean boom;
static int maxcoin;
static int sum;
public static void main(String[] args) throws FileNotFoundException {
/* Scanner sc= new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/plane"));
while (true) {
N = sc.nextInt();
if (N == 0) {
break;
}
data = new int[N][5];
boom = true;
x = N - 1;
y = 2;
maxcoin = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < 5; j++) {
data[i][j] = sc.nextInt();
}
}
if (data[x][y] == 1) {
sum = 1;
} else {
sum = 0;
}
dfs(0);
System.out.println(maxcoin);
}
}
private static void dfs(int step) {
if (step == (N-1)) {
if (maxcoin < sum) {
maxcoin = sum;
}
return;
}
if (sum + (N - step - 1) < maxcoin) {
return;
}
// zuo
if (y - 1 >= 0 && x - 1 >= 0) {
if ((data[x - 1][y - 1] == 2 && sum - 1 >= 0)
|| data[x - 1][y - 1] != 2) {
if (data[x - 1][y - 1] == 2) {
sum--;
}
if (data[x - 1][y - 1] == 1) {
sum++;
}
x -= 1;
y -= 1;
dfs(step + 1);
x += 1;
y += 1;
if (data[x - 1][y - 1] == 2) {
sum++;
}
if (data[x - 1][y - 1] == 1) {
sum--;
}
}
}
// you
if (y + 1 < 5 && x - 1 >= 0) {
if ((data[x - 1][y + 1] == 2 && sum - 1 >= 0)
|| data[x - 1][y + 1] != 2) {
if (data[x - 1][y + 1] == 2) {
sum--;
}
if (data[x - 1][y + 1] == 1) {
sum++;
}
x -= 1;
y += 1;
dfs(step + 1);
x += 1;
y -= 1;
if (data[x - 1][y + 1] == 2) {
sum++;
}
if (data[x - 1][y + 1] == 1) {
sum--;
}
}
}
// budong
if (x - 1 >= 0) {
if ((data[x - 1][y] == 2 && sum - 1 >= 0) || data[x - 1][y] != 2) {
if (data[x - 1][y] == 2) {
sum--;
}
if (data[x - 1][y] == 1) {
sum++;
}
x -= 1;
dfs(step + 1);
x += 1;
if (data[x - 1][y] == 2) {
sum++;
}
if (data[x - 1][y] == 1) {
sum--;
}
}
}
// zhandan
if (boom) {
int n = 0;
int k = x;
while (k - 1 >= 0) {
k--;
n++;
for (int i = 0; i < 5; i++) {
if (data[k][i] == 2) {
data[k][i] = 3;
}
}
if (n == 5) {
break;
}
}
boom = false;
dfs(step);
boom = true;
n = 0;
k = x;
while (k - 1 >= 0) {
k--;
n++;
for (int i = 0; i < 5; i++) {
if (data[k][i] == 3) {
data[k][i] = 2;
}
}
if (n == 5) {
break;
}
}
}
}
}
、、input
8
0 1 0 1 2
1 1 0 1 0
2 2 1 0 1
1 2 0 2 1
0 1 0 1 2
1 1 0 1 0
2 2 1 0 1
1 2 0 2 1
7
1 2 0 0 1
2 0 0 1 0
0 1 2 0 1
1 0 0 2 1
0 2 1 0 1
0 1 2 2 2
1 0 1 1 0
12
1 2 1 0 1
2 0 1 0 1
2 2 2 2 1
1 1 1 1 0
0 0 0 1 0
0 1 0 2 1
1 2 1 2 0
1 1 1 2 2
2 0 0 1 0
1 0 1 0 0
0 1 0 2 2
0 0 1 2 1
12
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
12
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
12
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
6
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
1 1 1 1 1
2 2 2 2 2
7
2 2 2 2 2
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 2 2 2 0
7
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
1 1 1 1 1
2 2 2 2 2
0
output
7
6
11
0
12
0
1
1
1