import std.stdio, std.string, std.conv, std.range;
import std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, std.random, core.bitop;

enum inf = 1_001_001_001;
enum infl = 1_001_001_001_001_001_001L;



void main() {
    while (true) {
        int H;
        scan(H);
        if (!H) return;
        auto g = iota(H).map!(i => readln.split.to!(int[])).array;
        auto ans = solve(H, g);
        writeln(ans);
    }
}

int solve(int H, int[][] g) {
    int res;

    bool fine = false;

    while (!fine) {
        fine = true;

        auto cnt = new int[][](H, 5);
        auto gc = new int[][](H, 5);

        foreach (i ; 0 .. H) {
            foreach (j ; 0 .. 5) {
                gc[i][j] = g[i][j];
            }
        }

        foreach_reverse (i ; 0 .. H) {
            foreach (j ; 0 .. 3) {
                if (g[i][j] && g[i][j] == g[i][j+1] && g[i][j+1] == g[i][j+2]) {
                    cnt[i][j] = cnt[i][j+1] = cnt[i][j+2] = 1;
                    gc[i][j] = gc[i][j+1] = gc[i][j+2] = 0;
                    fine = false;
                }
            }
        }

        debug {
            writefln("%(%s\n%)", gc);
            writeln;
            //writefln("%(%s\n%)", cnt);
        }

        // スコア計算
        foreach (i ; 0 .. H) {
            foreach (j ; 0 .. 5) {
                res += g[i][j] * cnt[i][j];
            }
        }

        auto gg = new int[][](H, 5);

        foreach (j ; 0 .. 5) {
            int pos = H - 1;
            foreach_reverse (i ; 0 .. H) {
                if (gc[i][j] != 0) {
                    gg[pos][j] = gc[i][j];
                    pos--;
                }
            }
        }

        debug {
            writefln("%(%s\n%)", gg);
            writeln;
        }

        g = gg;
    }

    return res;
}



void scan(T...)(ref T args) {
    auto line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront;
    }
    assert(line.empty);
}

void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}

bool chmin(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x > arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

bool chmax(T, U...)(ref T x, U args) {
    bool isChanged;
    foreach (arg; args) if (x < arg) {
        x = arg;
        isChanged = true;
    }
    return isChanged;
}

void yes(bool ok, string y = "Yes", string n = "No") {
    return writeln(ok ? y : n);
}
