import std.stdio;
import std.ascii;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

const int W = 5;

void main() {
    int H;
    int[][] A;
    bool input() {
        scanf("%d\n", &H);
        if (H == 0) return false;
        A = new int[][H];
        foreach (i; 0 .. H) {
            A[i] = readln.chomp.split(" ").map!(to!int).array;
        }
        reverse(A);
        return true;
    }

    bool erase(ref int Ans) {
        auto D = new bool[][](H, W);
        foreach (h; 0 .. H) {
            foreach (i; 0 .. W) {
                int init = A[h][i];
                int c = 0;
                foreach (j; 0 .. W) {
                    if (i + j >= W) break;
                    if (A[h][i + j] != init) {
                        break;
                    }
                    c++;
                }
                if (c < 3) continue;
                foreach (j; 0 .. c) {
                    D[h][i + j] = true;
                }
            }
        }
        int[][] S = new int[][W];
        foreach_reverse (i; 0 .. H) {
            foreach (j; 0 .. W) {
                if (D[i][j]) {
                    Ans += A[i][j];
                } else {
                    S[j] ~= A[i][j];
                }
            }
        }
        auto nA = new int[][](H, W);
        foreach (i; 0 .. H) {
            foreach (j; 0 .. W) {
                if (S[j].empty) nA[i][j] = 0;
                else {
                    nA[i][j] = S[j].back;
                    S[j].popBack;
                }
            }
        }
        //foreach (l; nA.dup.reverse) writeln(l); writeln;
        if (A == nA) return false;
        A = nA;
        return true;
    }

    void solve() {
        int Ans = 0;
        while (erase(Ans)) {}
        writeln(Ans);
    }
    while (input) solve;
}