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 "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main(){

    for(int h; cin >> h, h; ){
        int ans = 0;
        vector<string>vec(h, "XXXXX");
        for(int y = h - 1; y >= 0; y--){
            for(int x = 0; x < 5; x++){
                cin >> vec[y][x];
            }
        }
        for(;;){
            int tmp = 0;
            vector<string>flg = vec;
            for(int y = 0; y < h; y++){
                for(int x = 0; x < 3; x++){
                    if(vec[y][x] == vec[y][x + 1] && vec[y][x + 1] == vec[y][x + 2]){
                        flg[y][x] = 'F';
                        flg[y][x + 1] = 'F';
                        flg[y][x + 2] = 'F';
                    }
                }
            }
            for(int x = 0; x < 5; x++){
                queue<char>Q;
                for(int y = 0; y < h; y++){
                    if(flg[y][x] == 'F'){
                        tmp += vec[y][x] - '0';
                    }
                    else{
                        Q.push(vec[y][x]);
                    }
                }
                for(int y = 0; y < h; y++){
                    vec[y][x] = '0';
                    if(!Q.empty()){
                        vec[y][x] = Q.front();
                        Q.pop();
                    }
                }
            }
            if(tmp)ans += tmp;
            else break;
        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n) rep(i,n) {cout<<a[i]<<' ';} cout<<endl
#define show2(a, n, m) rep(i, n) { rep(j, m) {cout << a[i][j] << ' ';} cout << endl;} 
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;
typedef long double ld;

const int INF = 1 << 30;
const long long INFL = 1LL << 60;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

void fills(vector<vi>& s) {
	int h = s.size();
	rep(j, 5) {
		int d = h-1;
		for(int i = h-1; i >= 0; i--) {
			if(s[i][j] == 0) continue;
			swap(s[i][j], s[d][j]);
			d--;
		}
	}
}

int delsl(vi& s) {
	int len = 0;
	int l = 0, r = 0;
	for(r = 1; r < 5; r++) {
		if(s[l] != s[r]) {
			if(len >= 3) break;
			else l = r;
		}
		else if(r - l >= 2) {
			chmax(len, r - l + 1);
		}
	}
	int ret = s[l] * len;
	for(int i = l; i < len + l; i++) s[i] = 0;
	return ret;
}

int dels(vector<vi>& s) {
	int ret = 0;
	int h = s.size();
	rep(i, h) ret += delsl(s[i]);
	return ret;
}

int score(vector<vi>& s) {
	int ret = 0;
	int add;
	while(add = dels(s)) {
		ret += add;
		fills(s);
	}
	return ret;
}

int main() {
//	cout << fixed << setprecision(15);
	int h;
	vi ans;
	while(true) {
		cin >> h;
		if(h == 0) break;
		vector<vi> s(h, vi(5));
		rep(i, h) rep(j, 5) cin >> s[i][j];
		ans.push_back(score(s));
	}
	for(auto i: ans) cout << i << endl;
}

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;}
      }
      if(vanish.second>=3){
	ok=true;
	score+=vanish.first*vanish.second;
	vanishment(vanish,i);
      }
    }
    fall();
  }
  cout<<score<<endl;
}

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