#include<bits/stdc++.h>

using namespace std;

int main(){
    int h;
    while(1){
        cin >> h;
        if(h==0) break;
        vector<vector<int>> V(5, vector<int>(2*h,-1));
        for(int i=0; i<h; i++){
            cin >> V[0][h-1-i] >> V[1][h-1-i] >> V[2][h-1-i] >> V[3][h-1-i] >> V[4][h-1-i];
        }

        // cout << "==============" << endl;
        // for(int i=0; i<5; i++){
        //     for(int j=0; j<2*h; j++){
        //         cout << V[i][j];
        //     }
        //     cout << endl;
        // }
        
        int score = 0;
        // 最大hステップ
        for(int t=0; t<h; t++){
            // 各ステップで点数を計算
            for(int row=0; row<h; row++){
                // ある行で消えるものを見つける
                if(V[0][row] == V[1][row] and V[1][row] == V[2][row] and V[2][row] == V[3][row] and V[3][row] == V[4][row]){
                    if(V[0][row]==-1) continue;
                    score+=V[0][row]*5;
                }
                else if(V[0][row] == V[1][row] and V[1][row] == V[2][row] and V[2][row] == V[3][row]){
                    if(V[0][row]==-1) continue;
                    score+=V[0][row]*4;
                }
                else if(V[1][row] == V[2][row] and V[2][row] == V[3][row] and V[3][row] == V[4][row]){
                    if(V[1][row]==-1) continue;
                    score+=V[1][row]*4;
                }
                else if(V[0][row] == V[1][row] and V[1][row] == V[2][row]){
                    if(V[0][row]==-1) continue;
                    score+=V[0][row]*3;
                }   
                else if(V[1][row] == V[2][row] and V[2][row] == V[3][row]){
                    if(V[1][row]==-1) continue;
                    score+=V[1][row]*3;
                }
                else if(V[2][row] == V[3][row] and V[3][row] == V[4][row]){
                    if(V[2][row]==-1) continue;
                    score+=V[2][row]*3;
                }
            }
            // 盤面を更新
            for(int row=h-1; row>=0; row--){
                // ある行で消えるものを見つける
                if(V[0][row] == V[1][row] and V[1][row] == V[2][row] and V[2][row] == V[3][row] and V[3][row] == V[4][row]){
                    if(V[0][row]==-1) continue;
                    // cout << "1" << endl;
                    V[0].erase(V[0].begin()+row), V[0].push_back(-1);
                    V[1].erase(V[1].begin()+row), V[1].push_back(-1);
                    V[2].erase(V[2].begin()+row), V[2].push_back(-1);
                    V[3].erase(V[3].begin()+row), V[3].push_back(-1);
                    V[4].erase(V[4].begin()+row), V[4].push_back(-1);
                }
                else if(V[0][row] == V[1][row] and V[1][row] == V[2][row] and V[2][row] == V[3][row]){
                    if(V[0][row]==-1) continue;
                    V[0].erase(V[0].begin()+row), V[0].push_back(-1);
                    V[1].erase(V[1].begin()+row), V[1].push_back(-1);
                    V[2].erase(V[2].begin()+row), V[2].push_back(-1);
                    V[3].erase(V[3].begin()+row), V[3].push_back(-1);
                }
                else if(V[1][row] == V[2][row] and V[2][row] == V[3][row] and V[3][row] == V[4][row]){
                    if(V[1][row]==-1) continue;
                    V[1].erase(V[1].begin()+row), V[1].push_back(-1);
                    V[2].erase(V[2].begin()+row), V[2].push_back(-1);
                    V[3].erase(V[3].begin()+row), V[3].push_back(-1);
                    V[4].erase(V[4].begin()+row), V[4].push_back(-1);
                }
                else if(V[0][row] == V[1][row] and V[1][row] == V[2][row]){
                    if(V[0][row]==-1) continue;
                    V[0].erase(V[0].begin()+row), V[0].push_back(-1);
                    V[1].erase(V[1].begin()+row), V[1].push_back(-1);
                    V[2].erase(V[2].begin()+row), V[2].push_back(-1);
                }   
                else if(V[1][row] == V[2][row] and V[2][row] == V[3][row]){
                    if(V[1][row]==-1) continue;
                    V[1].erase(V[1].begin()+row), V[1].push_back(-1);
                    V[2].erase(V[2].begin()+row), V[2].push_back(-1);
                    V[3].erase(V[3].begin()+row), V[3].push_back(-1);
                }
                else if(V[2][row] == V[3][row] and V[3][row] == V[4][row]){
                    if(V[2][row]==-1) continue;
                    V[2].erase(V[2].begin()+row), V[2].push_back(-1);
                    V[3].erase(V[3].begin()+row), V[3].push_back(-1);
                    V[4].erase(V[4].begin()+row), V[4].push_back(-1);
                }
            }
        }

        // 盤面を確認
        // cout << "==============" << endl;
        // for(int i=0; i<5; i++){
        //     for(int j=0; j<2*h; j++){
        //         cout << V[i][j];
        //     }
        //     cout << endl;
        // }

        // cout << "score: " << score << endl;
        cout << score << endl;
    }
}


Measured memory_bytes: 3144