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<map>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;

int main() {
	int a[10][5];
	int b;
	while (cin >> b, b) {
		for (int c = 0; c < b; c++) {
			for (int d = 0; d < 5; d++) {
				scanf("%d", &a[c][d]);
			}
		}
		bool S;
		int sum = 0;
		do {
			S = false;
			bool k[10][5]{};
			for (int c = 0; c < b; c++) {
				for (int d = 0; d <= 2; d++) {
					if (a[c][d] == a[c][d + 1]&&a[c][d] == a[c][d + 2] && a[c][d] != 0) {
						k[c][d] = k[c][d + 1] = k[c][d + 2] = true;
						S = true;
					}
				}
			}
			for (int c = 0; c < 5; c++) {
				for (int d = 0; d < b; d++) {
					if (k[d][c]) {
						sum += a[d][c];
						for (int e = d; e > 0; e--) {
							a[e][c] = a[e - 1][c];
						}
						a[0][c] = 0;
					}
				}
			}
		} while (S);
		cout << sum << endl;
	}
}

C++: #include <cstdio>
#include <cstring>
#include <climits>
using namespace std;

int h,map[10][5];
int main(){
    while(scanf("%d",&h),h){
		int ans=0;		
		for(int i=0;i<10;i++)
			for(int j=0;j<5;j++)
				if(i<h)
		 			scanf("%d",&map[h-i-1][j]);
		 		else 
		 			map[i][j]=0;
		bool boo=true;
		while(boo){
			boo=false;
			for(int z=0;z<10;z++){
			for(int i=0;i<h;i++){
				for(int j=0;j<5;j++)
					if(map[i][j]==0){
						for(int k=i+1;k<h;k++){
							map[k-1][j]=map[k][j];
							map[k][j]=0;
						}					
					}
			}
			}
			/*
			for(int i=0;i<h;i++){
				for(int j=0;j<5;j++){
					printf("%d ",map[i][j]);
				}
				printf("\n");
			}
			printf("\n");
			*/
			for(int i=0;i<h;i++){
			 	int a=-1,b=0,t=0;
			 	for(int j=0;j<5;j++){
			 		if(map[i][j]==a){
			 			t++;
			 		}else if(t<3){
			 			a=map[i][j];
			 			b=j;
			 			t=1;
			 		}else break;
			 	}
			 	
			 	if(t>=3){
			 		if(a==0)continue;
			 		boo=true;
			 		ans+=a*t;
			 		//printf("%d %d %d\n%d\n",a,b,t,ans);
			 		for(int j=0;j<t;j++)
			 			map[i][b+j]=0;
			 	}
			 }
		}
		printf("%d\n",ans);
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

int cnt(vector< vector<int> > a){
    int res=0;
    while(true){
        
        bool flag=false;
        REP(i,a.size()){
            int c=a[i][0];
            int cnt=0;
            int maxcnt=0;
            int maxc=-1;
            int g=-1;
            REP(j,5){
                if(c!=a[i][j]){
                    cnt=1;
                    c=a[i][j];
                }else{
                    cnt++;
                }
                if(maxcnt<cnt){
                    maxcnt=cnt;
                    maxc=c;
                    g=j;
                }
            }
            if(maxcnt>=3 && maxc!=0){
                flag=true;
                for(int j=g-maxcnt+1;j<5;j++){
                    if(a[i][j]==maxc)a[i][j]=0;
                }
                res+=maxcnt*maxc;
            }
        }
    
        if(!flag)break;
        
        int cnt=0;
        for(int i=(int)a.size()-1;i>=0;){
            REP(j,5){
                if(a[i][j]==0){
                    for(int k=i-1;k>=0;k--){
                        a[k+1][j]=a[k][j];
                        if(k==0)a[k][j]=0;
                    }
                }
            }
            if(cnt>12){
                cnt=0;
                i--;
            }
            cnt++;
        }
    }
    return res;
}

int main(){
    
    while(true){
        int n; cin>>n;
        if(n==0)break;
        vector< vector<int> > a(n,vector<int>(5));
        REP(i,n)REP(j,5)cin>>a[i][j];
        cout<<cnt(a)<<endl;
    }
    
    return 0;
}