#include <bits/stdc++.h>
using namespace std;

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (ll)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) {
        ll d = euclid(b, a % b, y, x);
        return y -= a / b * x, d;
    }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1)
            ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1);
        return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e)
            return Mod(1);
        Mod r = *this ^ (e / 2);
        r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

int isprime(int n) {
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0 && (i % 7 == 1 || i % 7 == 6) && (n / i % 7 == 1 || n / i % 7 == 6)) {
            return false;
        }
    }
    return true;
}

int toint(string s) {
    string b;
    copy_if(all(s), back_inserter(b), [](char c) { return c != ':'; });
    return stoi(b);
}

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    cin.exceptions(cin.failbit);

    for (;;) {
        int h;
        cin >> h;
        if (!h)
            break;

        vector<vi> v(h, vi(5, 0));
        for (int i = h - 1; i >= 0; i--)
            rep(j, 0, 5) cin >> v[i][j];

        int score = 0;
        while (true) {


        // cout << "---" << endl;
        // for (int i = h - 1; i >= 0; i--) {
        //     rep(j, 0, 5) cout << v[i][j] << " ";
        //     cout << endl;
        // }
        // cout << "---" << endl;

            bool del = 0;
            rep(i, 0, h) {
                int st = 0;
                for (int j = 1; j < 5; j++) {
                    if (v[i][j] != v[i][st]) {
                        if (j - st >= 3 && v[i][st]) {
                            // cout << st << ";" << j << endl;
                            for (int k = st; k < j; k++)
                                score += v[i][k], v[i][k] = 0;
                            del = 1;
                        }
                        st = j;
                    }
                }
                if (5 - st >= 3 && v[i][st]) {
                    // cout << st << ";" << 5 << endl;
                    for (int k = st; k < 5; k++)
                        score += v[i][k], v[i][k] = 0;
                    del = 1;
                }
            }
            if (!del)
                break;
            rep(j, 0, 5) {
                int ppos = 0;
                rep(i, 0, h) {
                    if (v[i][j]) {
                        v[ppos][j] = v[i][j]; if (i != ppos++) v[i][j] = 0;
                    }
                }
            }
        }

        cout << score << endl;
    }
}



Measured memory_bytes: 3156