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;

using ll = long long;

template<typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;

template<typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;

template<typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define REP2(i,s,e) for(int i=(int)(s);i<(int)(e);++i)
#define RREP(i,s,e) for(int i=(int)(s);i>=(int)(e);--i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin(),ie=(c).end();i!=ie;++i)
#define ALL(c) (c).begin(), (c).end()

const double PI = acos(-1);
const ll MOD = 1e9+7;

int n;
// string s;
VVi v;

void fall(int depth, int start, int end) {
    RREP(i, depth, 0) {
        if (i != 0) {
            REP2(j, start, end+1) {
                v[i][j] = v[i-1][j];
            }
        } else {
            REP2(j, start, end+1) {
                v[i][j] = -1;
            }
        }
    }
}

int getScore(int depth, int start, int end) {
    int x = v[depth][start];
    if (x == -1) return 0;
    if (end - start + 1 < 3) return 0;
    fall(depth, start, end);
    return x * (end - start + 1);
}

int main() {
    while (true) {
        cin >> n;
        if (n == 0) break;

        v = VVi(n, Vi(5));
        REP(i, n) REP(j, 5) cin >> v[i][j];
        int result = 0;

        while (true) {
            bool f = false;
            REP(i, n) {
                Vi u = v[i];
                int x = u[0], start = 0;
                REP2(j, 1, 5) {
                    if (u[j] != x) {
                        int score = getScore(i, start, j-1);
                        if (score > 0) f = true;
                        result += score;
                        x = u[j]; start = j;
                    }
                }
                int score = getScore(i, start, 4);
                if (score > 0) f = true;
                result += score;
            }
            if (!f) break;
        }
        std::cout << result << '\n';
    }

    return 0;
}

C++: #include<iostream>
using namespace std;
int x[1000][5],score,h;
int main(){
	while(true){
		score=0;
		cin>>h;if(h==0){break;}
		for(int i=0;i<h;i++){
			for(int j=0;j<5;j++){
				cin>>x[i][j];
			}
		}
		for(int times=0;times<100;times++){
			for(int i=0;i<5;i++){
				if(((x[i][0]==x[i][1])&&(x[i][2]==x[i][3]))&&((x[i][1]==x[i][2])&&(x[i][3]==x[i][4]))){
					score+=x[i][0]*5;
					x[i][0]=0;x[i][1]=0;x[i][2]=0;x[i][3]=0;x[i][4]=0;
				}
			}
			for(int i=0;i<h;i++){
				for(int j=0;j<2;j++){
					if(((x[i][j+0]==x[i][j+1])&&(x[i][j+2]==x[i][j+3]))&&(x[i][j+1]==x[i][j+2])){
						score+=x[i][j]*4;
						x[i][j+0]=0;x[i][j+1]=0;x[i][j+2]=0;x[i][j+3]=0;
					}
				}
			}
			for(int i=0;i<h;i++){
				for(int j=0;j<3;j++){
					if((x[i][j+0]==x[i][j+1])&&(x[i][j+1]==x[i][j+2])){
						score+=x[i][j]*3;
						x[i][j+0]=0;x[i][j+1]=0;x[i][j+2]=0;
					}
				}
			}
			for(int i=0;i<5;i++){
				for(int t=0;t<20;t++){
					for(int j=h-1;j>=0;j--){
						if(x[j][i]==0){
							for(int k=j;k>0;k--){
								x[k][i]=x[k-1][i];
							}
							x[0][i]=0;
						}
					}
				}
			}
			score+=0;
		}
		cout<<score<<endl;
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int H;
	cin >> H;

	while ( H != 0 ) {
		std::vector< std::vector<int> > puzz(H+2, std::vector<int>(5+2));
		std::vector<int> count(10,0);
		for (int i = 0; i < H+2; i++) {
			for (int j = 0; j < 5+2; j++) {
				if (i==0||i==H+1||j==0||j==5+1) {
					puzz[i][j] = -1;
				}
				else {
					cin >> puzz[H-i+1][j];
					count[puzz[H-i+1][j]]++;
				}
			}
		}

		bool chain = true;
		bool same = true;
		bool find = false;
		int n,l;
		int point = 0;
		while (chain) {
			chain = false;
			for (int i = 1; i < H+1; i++) {
				for (int j = 1; j < 5+1; j++) {
					if (count[puzz[i][j]] > 2) {
						n = puzz[i][j];
						l = 1;
						same = true;
						while (same) {
							if (n == puzz[i][j+l]) {
								l++;
							}
							else {
								same = false;
							}
						}
						if (l > 2) {
							for (int k = 0; k < l; k++) {
								puzz[i][j+k] = 0;
							}
							point += n * l;
							count[n] -= l;
							chain = true;
						}
					}
				}
			}

			for (int i = 1; i < H+1; i++) {
				for (int j = 1; j < 5+1; j++) {
					if (puzz[i][j] == 0) {
						find = false;
						for (int k = i+1; k < H+1 && !(find); k++) {
							if (puzz[k][j] != 0) {
								std::swap(puzz[i][j], puzz[k][j]);
								find = true;
							}
						}
					}
				}
			}
		}
		cout << point << endl;
		cin >> H;
	}

	return 0;
}