Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		h := io.NextInt()
		if h == 0 {
			break
		}
		p := make([][]int, h)
		for i := 0; i < h; i++ {
			p[i] = make([]int, 5)
			for j := 0; j < 5; j++ {
				p[i][j] = io.NextInt()
			}
		}
		ans := 0
		for {
			sc := puyo(p, h)
			if sc == 0 {
				break
			}
			fall(p, h)
			ans += sc
		}
		io.Println(ans)
	}
}

func puyo(p [][]int, h int) int {
	sc := 0
	for i := 0; i < h; i++ {
		for j := 0; j < 3; j++ {
			var cnt int
			for cnt = 1; j+cnt < 5; cnt++ {
				if p[i][j] != p[i][j+cnt] {
					break
				}
			}
			if cnt > 2 {
				sc += p[i][j]*cnt
				for k := 0; k < cnt; k++ {
					p[i][j+k] = 0
				}
			}
		}
	}
	return sc
}

func fall(p [][]int, h int) {
	for j := 0; j < 5; j++ {
		for k := 1; k < h; k++ {
			for i := h - 1; i >= k; i-- {
				if p[i][j] == 0 {
					p[i][j] = p[i-1][j]
					p[i-1][j] = 0
				}
			}
		}
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>

using namespace std;

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)


#define all all(v) v.begin(),v.end()
#define INF i<<30
#define mp make_pair
#define pb push_back

#define fi first
#define se second

#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
typedef vector<pair<int, int> > vp;

typedef long long ll;
int s[30][10];
int H,W;
int ans = 0;
void drop(int iy,int x){
	for (int y = iy; y > 0; --y)
	{
		s[y][x] = s[y-1][x];
	}
}
void kesu(int y){
	int c = 1;
	int tmp = 0;
	for(int x = 0;x<7;x++){
		if(s[y][x]==tmp) c++;
		else{
			if(c>=3){
				if(tmp==0) continue;
				ans += tmp*c;
				for (int tx = x-1; tx > x-1-c ; --tx)
				{
					s[y][tx]=0;
				}
			}
			c = 1;
			tmp = s[y][x];
		}
	}
	// cout << "kesu:"<<ans<< endl;
}
int main(){
	while(1){
		cin >> H;
		if(H==0) break;
		ans = 0;
		rep(i,H+1){
			if(i==0){
				rep(j,7){
					s[i][j] = 0;
				}
			}else{
				rep(j,7){
					if(j==0) s[i][j]=0;
					else if(j==6) s[i][j]=0;
					else{
						cin >> s[i][j];
					} 
				}
			}
			
		}
		// rep(i,H+1){
		// 	rep(j,7){
		// 		cout<< s[i][j];
		// 	}
		// 	cout << endl;
		// }
		while(1){
			int tmpans = ans;
			rep(y,H+1){
				kesu(y);
			
			}
			// cout << tmpans << " " << ans << endl;
			if(tmpans==ans) break;
			for (int y = 1; y < H+1; ++y)
			{
				for(int x=1;x<6;++x){
					if(s[y][x]==0){
						drop(y,x);
					}
				}
			}
		}

		// rep(i,H+1){
		// 	rep(j,7){
		// 		cout<< s[i][j];
		// 	}
		// 	cout << endl;
		// }
		cout << ans << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <climits>
#include <bitset>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define fin(ans) cout<<(ans)<<endl
#define P 1000000007
#define STI(s) atoi(s.c_str())//string to int
#define mp(p,q) make_pair(p,q)
#define Sort(a) sort(a.begin(),a.end())
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
const int INF = INT_MAX;
const long long LLINF = 1LL<<60;
//g++ -std=c++1z temp.cpp
//./a.out


int main(void){ios::sync_with_stdio(false);cin.tie(0);
//////////////////////////////////////////////////////
    
    int h;
    while(1){
        cin>>h;
        if(h==0)break;
        vvi v(2*h,vector<int> (5,-1));
        for(int i=h;i<2*h;i++){
            rep(j,5){
                cin>>v[i][j];
            }
        }
        int sum=0;

        while(1){
            bool ch=false;
            for(int i=h;i<2*h;i++){
                int l=0,r=0;
                if(v[i][2]==-1)continue;
                while(1-l>=0&&v[i][2]==v[i][1-l])l++;
                while(3+r<=5&&v[i][2]==v[i][3+r])r++;
                if(l+r>=2){
                    rep(j,l)v[i][1-j]=-1;
                    rep(j,r)v[i][3+j]=-1;
                    sum+=(l+r+1)*v[i][2];
                    ch=true;
                    v[i][2]=-1;
                }
                
            }
            
            for(int n=0;n<200;n++){
                for(int i=2*h-1;i>=h;i--){
                    for(int j=0;j<5;j++){
                        if(v[i][j]==-1&&v[i-1][j]!=-1){
                            v[i][j]=v[i-1][j];
                            v[i-1][j]=-1;
                        }
                    }
                }
            }
            
/*
            cout<<endl;
            for(int i=h;i<2*h;i++){
                for(int j=0;j<5;j++)cout<<v[i][j]<<" ";
                cout<<endl;
            }
            cout<<endl;
*/
            if(!ch)break;

        }
        
        
        cout<<sum<<endl;
    }
//////////////////////////////////////////////////////
return 0;}