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<vector>
using namespace std;

int main(){
    int H;
    int i,j;
    int memo[10][5];
    bool update;
    int score;

    cin >> H;
    while(H>0){
        score = 0;
        vector<int> tow[5];
        vector<int>::iterator t;

        //input
        for(i=0; i<H; i++){
            for(j=0; j<5; j++){
                cin >> memo[i][j];
            }
        }

        //make tower
        for(j=0; j<5; j++){
            for(i=H-1; i>=0; i--){
                tow[j].push_back(memo[i][j]);
            }
        }

        //shoumetsu hantei
        update = 1;
        while(update){
            update=0;
            vector<int> dou[10];
            for(i=0; i<H; i++){
                for(j=0; j<3; j++){
                    if(tow[j][i]==tow[j+1][i]&&tow[j][i]>0){
                        if(tow[j+1][i]==tow[j+2][i]){
                            update = 1;
                            if(j<2){
                                if(tow[j+2][i]==tow[j+3][i]){
                                    if(j<1){
                                        if(tow[j+3][i]==tow[j+4][i]){
                                            dou[i].push_back(j);
                                            dou[i].push_back(j+4);
                                            dou[i].push_back(tow[j][i]);
                                            break;
                                        }
                                    }
                                    dou[i].push_back(j);
                                    dou[i].push_back(j+3);
                                    dou[i].push_back(tow[j][i]);
                                    break;
                                }
                            }
                            dou[i].push_back(j);
                            dou[i].push_back(j+2);
                            dou[i].push_back(tow[j][i]);
                            break;
                        }
                    }
                }
            }

            //add score
            for(i=0; i<H; i++){
                if(!dou[i].empty()){
                    score += (dou[i][1]-dou[i][0]+1)*dou[i][2];
                }
            }

            //erase
            for(i=H-1; i>=0; i--){
                if(!dou[i].empty()){
                    for(j=dou[i][0]; j<=dou[i][1]; j++){
                        tow[j].erase(tow[j].begin()+i);
                    }
                }
            }

            //add -1(none)
            for(j=0; j<5; j++){
                while(tow[j].size()<H){
                    tow[j].push_back(-1);
                }
            }
        }

        //answer
        cout << score << endl;

        //input
        cin >> H;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007

#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define MOD(x) (x%(mod))
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

const int none=0;

int p[15][6];
int n;

void debug_print(){
  cout<<endl;
  rep(i,n){
    rep(j,5)cout<<p[i][j]<<" ";
    cout<<endl;
  }
}

void init(){
  rep(i,n)rep(j,5)p[i][j]=none;
}

void fall(){
  for(int j=0;j<5;j++){
    for(int i=n-1;i>=1;i--){
      for(int k=n-1;k>=1;k--)if(p[k][j]==none)swap(p[k][j],p[k-1][j]);
    }
  }
}

void vanishment(pi vanish,int i){
  bool ok=false;
  rep(j,5){
    if(vanish.first==p[i][j]&&vanish.first==p[i][j+1])ok=true;
    if(vanish.second!=0&&p[i][j]==vanish.first&&ok){
      p[i][j]=none;
      vanish.second--;
    }
  }
}

void solve(){
  pi vanish;
  int score=0;
  bool ok=true;
  while(ok){
    ok=false;
    rep(i,n){
      vanish.first=p[i][0];vanish.second=1;
      REP(j,1,5){
	if(p[i][j]==vanish.first&&p[i][j]!=none)vanish.second++;
	else if(vanish.second<=2){vanish.first=p[i][j];vanish.second=1;}
	else if(p[i][j]!=vanish.first&&vanish.second>=3)break;
      }
      if(vanish.second>=3){
	ok=true;
	score+=vanish.first*vanish.second;
	vanishment(vanish,i);
	if(debug)cout<<"score: "<<score<<endl;
      }
    }
    if(debug)cout<<"before";
    if(debug)debug_print();
    fall();
    if(debug)cout<<"after";
    if(debug)debug_print();
  }
  cout<<score<<endl;
}

int main(){
  while(cin>>n){
    if(n==0)break;
    init();
    rep(i,n)rep(j,5)cin>>p[i][j];
    solve();
  }
}