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;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
#define fore(i,a,b) for(ll i=(ll)(a);i<=(ll)(b);++i)
#define rep(i,n) fore(i,0,(n)-1)
#define rfore(i,a,b) for(ll i=(ll)(b);i>=(ll)(a);--i)
#define rrep(i,n) rfore(i,0,(n)-1)
#define all(x) (x).begin(),(x).end()
const ll INF=1001001001;
const ll LINF=1001001001001001001;
const ll D4[]={0,1,0,-1,0};
const ll D8[]={0,1,1,0,-1,-1,1,-1,0};
template<class T>
bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}

void solve(){
    while(1){
        ll h;cin>>h;
        if(h==0)return;

        vvl a(h,vl(5));
        rep(i,h)rep(j,5)cin>>a[i][j];
        ll ans=0;
        while(1){
            ll tmp=0;
            rep(i,h)rep(j,5){
                if(a[i][j]==0)continue;
                ll k=j;
                while(k<4&&a[i][k+1]==a[i][j])k++;
                if(k-j>=2){
                    tmp+=a[i][j]*(k-j+1);
                    fore(l,j,k)a[i][l]=0;
                }
                j=k;
            }
            if(tmp==0)break;
            ans+=tmp;
            rep(i,5){
                ll idx=h-1;
                rrep(j,h){
                    if(a[j][i]){
                        if(j!=idx){
                            a[idx][i]=a[j][i];
                            a[j][i]=0;
                        }
                        idx--;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    solve();
}

C++: #include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (1 << 21);
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };

// ──────────────────────────────────────────────────────────────────

int t[10][5], h;

int check() {
	int row, col, len, i;
	int score = 0;

	for (row = 0; row < h; row++) {
		for (col = 0; col <= 2; col++) {
			for (len = 1; col + len <= 4; len++) {
				if (t[row][col] != t[row][col + len]) break;
			}
			if (len <= 2) continue;
			score += t[row][col] * len;
			for (int i = 0; i < len; ++i) t[row][col + i] = 0;
		}
	}
	return score;
}

void down() {
	int col, row, min;
	for (col = 0; col < 5; ++col) {
		for (min = 1; min < h; ++min) {
			for (row = h - 1; row >= min; row--) {
				if (t[row][col] == 0) {
					t[row][col] = t[row - 1][col];
					t[row - 1][col] = 0;
				}
			}
		}
	}
}

int main() {
	int i, j;
	int score, cs;
	while (true) {
		cin >> h;
		if (h == 0) break;

		for (i = 0; i < h; ++i) {
			for (j = 0; j < 5; ++j) {
				cin >> t[i][j];
			}
		}
		score = 0;
		while (true) {
			cs = check();
			//cout << cs << endl;
			if (cs == 0) {
				cout << score << endl;
				break;
			}
			score += cs;
			down();
		}
	}
}

C++: #include<iostream>
#include<vector>
using namespace std;

class game{
private:
  vector<vector<int> > map;
  int score;
  int size;
public:
  game(int s){
    size=s;
    map.resize(size);
    for(int i=0;i<size;i++){
      map[i].resize(5);
    }
    score=0;
  }

  void setnum(int num,int i,int j){
    map[i][j]=num;
  }

  void deleteline(){
    for(int i=0;i<size;i++){
      int c=1;
      int delnum=0;
      for(int j=0;j<4;j++){
	if(map[i][j]==map[i][j+1]) c++;
	else c=1;
	if(c>=3){
	  delnum=map[i][j];
	}
      }
      for(int j=0;j<5;j++){
	if(map[i][j]==delnum){map[i][j]=0;score+=delnum;}
      }
    }
  }

  void falldown(){
    for(int c=0;c<size;c++){
      for(int i=0;i<size-1;i++){
	for(int j=0;j<5;j++){
	  if(map[i+1][j]==0){ map[i+1][j]=map[i][j]; map[i][j]=0;} 
	}
      }
    }
  }

  int calc(){
    int bf = -1;
    while(bf != score){
      bf = score;
      deleteline();
      falldown();
    }
    return score;
  }

};

int main(){
  int t=1;
  while(cin>>t){
    if(t == 0) break;
    game g(t);
    for(int i=0;i<t;i++){
      for(int j=0;j<5;j++){
	int num;
	cin>>num;
	g.setnum(num,i,j);
      }
    }
    cout<<g.calc()<<endl;

  }
  return 0;
}