import java.util.*;
public class Main {
private static final int W = 5;
public static void main
(String[] args
) { final Scanner sc
= new Scanner
(System.
in); while(sc.hasNext()) {
final int H = sc.nextInt();
if(H == 0)
return;
final int[][] css = new int[H][W];
for(int i = 0; i < H; i++)
for(int j = 0; j < W; j++)
css[i][j] = sc.nextInt();
int ans = 0;
boolean flag = true;
while(flag) {
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++)
}
flag = false;
for(int i = 0; i < H; i++)
for(int j = 0; j < W - 2; j++)
if(css[i][j] > 0 && css[i][j+1] == css[i][j] && css[i][j+2] == css[i][j]){
final int c = css[i][j];
css[i][j] = css[i][j + 1] = css[i][j + 2] = 0;
ans += 3 * c;
j += 3;
flag = true;
while(j < W && css[i][j] == c) {
css[i][j] = 0;
ans += c;
j++;
}
}
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++)
}
for(int j = 0; j < W; j++) {
int k = H - 1;
for(int i = H - 1; i >= 0; i--)
if(css[i][j] > 0)
css[k--][j] = css[i][j];
while(k >= 0)
css[k--][j] = 0;
}
}
}
}
}
aW1wb3J0IGphdmEudXRpbC4qOwpwdWJsaWMgY2xhc3MgTWFpbiB7CiAgcHJpdmF0ZSBzdGF0aWMgZmluYWwgaW50IFcgPSA1OwogIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKICAgIGZpbmFsIFNjYW5uZXIgc2MgPSBuZXcgU2Nhbm5lcihTeXN0ZW0uaW4pOwogICAgd2hpbGUoc2MuaGFzTmV4dCgpKSB7CiAgICAgIGZpbmFsIGludCBIID0gc2MubmV4dEludCgpOwogICAgICBpZihIID09IDApCiAgICAgICAgcmV0dXJuOwogICAgICBmaW5hbCBpbnRbXVtdIGNzcyA9IG5ldyBpbnRbSF1bV107CiAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBIOyBpKyspCiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IFc7IGorKykKICAgICAgICAgIGNzc1tpXVtqXSA9IHNjLm5leHRJbnQoKTsKICAgICAgaW50IGFucyA9IDA7CiAgICAgIGJvb2xlYW4gZmxhZyA9IHRydWU7CiAgICAgIHdoaWxlKGZsYWcpIHsKU3lzdGVtLmVyci5wcmludGxuKCJAQEAiKTsKZm9yKGludCBpID0gMDsgaSA8IEg7IGkrKykgewogIGZvcihpbnQgaiA9IDA7IGogPCBXOyBqKyspCiAgICBTeXN0ZW0uZXJyLnByaW50KGNzc1tpXVtqXSk7CiAgU3lzdGVtLmVyci5wcmludGxuKCk7Cn0KICAgICAgICBmbGFnID0gZmFsc2U7CiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8IEg7IGkrKykKICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCBXIC0gMjsgaisrKQogICAgICAgICAgICBpZihjc3NbaV1bal0gPiAwICYmIGNzc1tpXVtqKzFdID09IGNzc1tpXVtqXSAmJiBjc3NbaV1baisyXSA9PSBjc3NbaV1bal0pewogICAgICAgICAgICAgIGZpbmFsIGludCBjID0gY3NzW2ldW2pdOwogICAgICAgICAgICAgIGNzc1tpXVtqXSA9IGNzc1tpXVtqICsgMV0gPSBjc3NbaV1baiArIDJdID0gMDsKICAgICAgICAgICAgICBhbnMgKz0gMyAqIGM7CiAgICAgICAgICAgICAgaiArPSAzOwogICAgICAgICAgICAgIGZsYWcgPSB0cnVlOwogICAgICAgICAgICAgIHdoaWxlKGogPCBXICYmIGNzc1tpXVtqXSA9PSBjKSB7CiAgICAgICAgICAgICAgICBjc3NbaV1bal0gPSAwOwogICAgICAgICAgICAgICAgYW5zICs9IGM7CiAgICAgICAgICAgICAgICBqKys7CiAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ClN5c3RlbS5lcnIucHJpbnRsbigiKioqIik7CmZvcihpbnQgaSA9IDA7IGkgPCBIOyBpKyspIHsKICBmb3IoaW50IGogPSAwOyBqIDwgVzsgaisrKQogICAgU3lzdGVtLmVyci5wcmludChjc3NbaV1bal0pOwogIFN5c3RlbS5lc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