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 <vector>
#include <iostream>
using namespace std;
int main(){
	int h;
	while(cin >> h,h){
		vector<int> mp(6*h);
		for(int i=0;i<h;i++){
			cin >> mp[6*i] >> mp[6*i+1] >> mp[6*i+2] >> mp[6*i+3] >> mp[6*i+4];
			mp[6*i + 5] = -1;
		}
		bool del = true;
		int ct =0;
		while(del){
			del = false;
			int ketsu = 0;
			int point = 0;
			for(int i=0;i<6*h-2;i++){
				if(mp[i] == mp[i+1] && mp[i+1] == mp[i+2] && mp[i] !=0){
					if(ketsu == 0){
						ketsu = 3;
						point = mp[i];
					}else{
						ketsu++;
					}
				}else if(ketsu > 0){
					ct += point * ketsu;
					del = true;
					for(int ki = 0;ki<ketsu;ki++){
						mp[i + 1 - ki] = 0;
					}
					ketsu = 0;//init
					point = 0;
				}
			}
			/*			for(int i = 0;i<6*h;i++){
				cout << mp[i] << " ";
				if(i%6 == 0 && i>0)cout << endl;
			}
			cout << endl;*/
			if(del){//fall
				for(int i=0;i<5;i++){//line number
					int fall_ct = 0;
					for(int j=h-1;j>=0;j--){//direction is up
						if(mp[j*6 + i] == 0){
							fall_ct++;
						}else if(fall_ct > 0){
							mp[(j + fall_ct)*6 + i ] = mp[j * 6 + i];
							mp[j*6 + i ] = 0;
						}
					}
				}
				
			}
			
			/*			for(int i = 0;i<6*h;i++){
				cout << mp[i] << " ";
				if(i%6 == 0 && i>0)cout << endl;
			}
			cout << endl;*/
			}

		cout << ct << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main() {
    ll H;
    while(cin >> H) {
        if(H == 0) return 0;

        vvll c(20, vll(5, 0));

        rep(i, H) rep(j, 5) cin >> c[H-1-i][j];

        ll ans = 0;
        bool end = false;

        while(!end) {
            vector<vector<bool>> blank(20, vector<bool>(5, false));
            end = true;

            rep(i, H) {
                ll left = 0, right = 0, cnt = 1;
                for(ll j = 1; j < 5; j++) {
                    if(c[i][j-1] == c[i][j] && c[i][j] != 0) {
                        cnt++;
                        right = j;
                        if(j == 4 && cnt >= 3) {
                            ans += c[i][j] * cnt;
                            end = false;
                        }
                    }
                    else {
                        if(cnt >= 3) {
                            ans += c[i][j-1] * cnt;
                            end = false;
                            break;
                        }
                        else {
                            left = right = j;
                            cnt = 1;
                        }
                    }
                }
                if(cnt >= 3) for(ll k = left; k <= right; k++) blank[i][k] = true;
            }

            rep(j, 5) rep(i, H) {
                ll k = i;
                while(blank[k][j]) {
                    k++;
                }
                c[i][j] = c[k][j];
                if(c[k][j] != 0) blank[k][j] = true;
            }
        }
        cout << ans << endl;
    }
}

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

using namespace std;

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		int counter = 0;
		vector< vector<int> > hyou(n, vector<int>(5));
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < 5; j++) {
				cin >> hyou[i][j];
			}
		}
		bool hantei = true;
		while (hantei) {
			hantei = false;
			for (int i = n-1; i >= 0; i--) {
				for (int j = 0; j < 3; j++) {
					if(hyou[i][j] != 0 && hyou[i][j] == hyou[i][j+1] && hyou[i][j] == hyou[i][j+2]) {
						hantei = true;
						if (j < 2 && hyou[i][j] == hyou[i][j+3]) {
							if (j < 1 && hyou[i][j] == hyou[i][j+4]) {
								counter += (hyou[i][j] * 5);
								hyou[i][0] = hyou[i][1] = hyou[i][2] = hyou[i][3] = hyou[i][4] = 0;
								break;
							}
							counter += (hyou[i][j] * 4);
							hyou[i][j] = hyou[i][j+1] = hyou[i][j+2] = hyou[i][j+3] = 0;
							break;
						}
						counter += (hyou[i][j] * 3);
						hyou[i][j] = hyou[i][j+1] = hyou[i][j+2] = 0;
						break;
					}
				}
			}
			for (int i = 0; i < 5; i++) {
				for (int j = n-1; j >= 0; j--) {
					if (hyou[j][i] == 0) {
						for (int k = j-1; k >= 0; k--) {
							if (hyou[k][i] != 0) {
								hyou[j][i] = hyou[k][i];
								hyou[k][i] = 0;
								break;
							}
						}
					}
				}
			}
		}
		cout << counter << endl;
	}
	return 0;
}