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;

#define rep(i, n) for (int i = 0; i < (int) n; i++)
#define reps(i, n, m) for (int i = n; i < (int) m; i++)

int h, matrix[20][6];

bool init()
{
	cin >> h;
	if (h == 0) return false;
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j < 5; j++) {
			matrix[i][j] = 0;
		}
	}
	for (int i = 0; i < h; i++) {
		cin >> matrix[h-i-1][0] >> matrix[h-i-1][1] >> matrix[h-i-1][2] >> matrix[h-i-1][3] >> matrix[h-i-1][4];
		matrix[i][5] = 0;
	}
	return true;
}

int calc(int a, int b) {
	return a * (b + 100) / 100;
}

void solve()
{
	int ans = 0;
	while (1) {
		int cflag = 0;
		for (int i = 0; i < h; i++) {
			int snode = 0, scnt = 1;
			for (int j = 1; j < 6; j++) {
				if (matrix[i][snode] == matrix[i][j] && matrix[i][snode] != 0) scnt++;
				else {
					if (scnt >= 3) {
						ans += scnt * matrix[i][snode];
						cflag = 1;
						//cout << snode<<endl;
						for (int k = 0; k < scnt; k++) matrix[i][snode+k] = 0;
						break;
					}
					else{
						scnt = 1;
						snode = j;
					}
				}
			}
		}
		if (cflag == 0) break;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < 5; j++) {
				for (int k = i; k > 0; k--) {
					if (matrix[k-1][j] == 0) swap(matrix[k-1][j], matrix[k][j]);
					else break;
				}
			}
		}	
	}
	cout << ans << endl;
}

int main()
{
	while (init()) {
		solve();
	}
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
using namespace std;

template <typename X, typename T>
auto vectors(X x, T a) {
	return vector<T>(x, a);
}

template <typename X, typename Y, typename Z, typename... Zs>
auto vectors(X x, Y y, Z z, Zs... zs) {
	auto cont = vectors(y, z, zs...);
	return vector<decltype(cont)>(x, cont);
}

template<typename T>
ostream& operator << (ostream& os, vector<T>& v){
	rep(i,v.size()){ os << v[i] << (i == v.size() - 1 ? "" : " "); } return os;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& v){
	for(T& x: v){ is >> x; } return is;
}

const int INF = 1e8;
const int H = 10;
const int W = 5;


int checkLine(int x, int m[W]){
	if(m[x] == INF) return 0;

	int cnt = 0;
	range(i,x,W){
		if(m[i] == m[x]) cnt++;
		else break;
	}
	assert(cnt >= 0 and cnt <= 5);
	return cnt;
}

void drop(int h, int m[H][W], int y, int x){
	if(y + 1 >= h or m[y][x] == INF) return;

	if(m[y + 1][x] == INF){
		m[y + 1][x] = m[y][x];
		m[y][x] = INF;
	}
}

int deleteLine(int h, int m[H][W]){
	int res = 0;
	rep(i,h){
		rep(j,W){
			int cnt = checkLine(j, m[i]);
			if(cnt >= 3){
				rep(k,cnt){
					res += m[i][j + k];
					m[i][j + k] = INF;
				}
			}
		}
	}

	rep(_,10){
		rep(i,h){
			rep(j,W){
				drop(h,m,i,j);
			}
		}
	}

	return res;
}

int main(){
	int h;
	while(cin >> h,h){
		int m[H][W];
		rep(i,h) rep(j,W) cin >> m[i][j];

		int ans = 0;
		while(true){
			int cnt = deleteLine(h, m);
			if(cnt == 0) break;
			ans += cnt;
		}

		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>
typedef std::vector<int>  VI;

int H;
std::vector< VI > board(10, VI(5, -1));

int erase() {
    int i, j, k, x;
    int score = 0;
    for(i = 0; i < H; ++i) {
        for(j = 0; j < 3; ++j) {
            k = j + 1;
            x = board[i][j];
            if(x == -1) continue;
            while(k < 5 && board[i][k] == x) ++k;
            --k;
            if(k - j >= 2) {
                score += (k - j + 1) * x;
                while(k >= j) {
                    board[i][k] = -1;
                    --k;
                }
            }
        }
    }
    return score;
}

bool update() {
    int i, j, k;
    bool isUpdate = false;;
    for(j = 0; j < 5; ++j) {
        for(i = H-1; i >= 0; --i) {
            if(board[i][j] == -1 || i == H-1 || board[i+1][j] != -1) continue;
            k = i+1;
            while(k < H && board[k][j] == -1) {
                isUpdate = true;
                board[k][j] = board[k-1][j];
                board[k-1][j] = -1;
                ++k;
            }
        }
    }
    return isUpdate;
}

int main() {
    int i, j;
    int score = 0;
    while(std::cin >> H, H != 0) {
        for(i = 0; i < H; ++i) {
            for(j = 0; j < 5; ++j) {
                std::cin >> board[i][j];
            }
        }
       
       score = 0;
        do {
            score += erase();
        } while(update());

        std::cout << score << std::endl;
    }
}