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<iostream>
#include<vector>
#include<string.h>
using namespace std;
int main(){
    vector<vector<int> >bo(5);
    int n;
    while(cin>>n,n){
        for(int i=0;i<5;i++)
            bo[i].clear();
        for(int i=0;i<n;i++){
            for(int j=0;j<5;j++){
                int t;
                cin>>t;
                bo[j].push_back(t);
            }
        }
        int tot=0;
        while(1){
            vector<pair<pair<int,int>,int> >vec;
            int so=0;
            for(int i=0;i<3;i++){
                for(int j=0;j<bo[i].size();j++){
                    if(j<bo[i+1].size()&&j<bo[i+2].size()){
                        if(bo[i][bo[i].size()-1-j]==bo[i+1][bo[i+1].size()-1-j]&&bo[i+1][bo[i+1].size()-1-j]==bo[i+2][bo[i+2].size()-1-j]){
                            so=1;
                            int x=bo[i][bo[i].size()-1-j];
                            int k;
                            for(k=i;k<5;k++){
                                if(bo[k][bo[k].size()-1-j]==x){
                                    tot+=x;
                                    bo[k][bo[k].size()-1-j]=100+k;
                                }
                                else {
                                    break;
                                }
                            }
                            vec.push_back(make_pair(make_pair(i, k),j));
                        }
                    }
                        
                }
            }
            for(int i=0;i<5;i++){
                for(int j=0;j<bo[i].size();j++){
                    if(bo[i][j]>30){
                        bo[i].erase(bo[i].begin()+j, bo[i].begin()+j+1);
                        j--;
                    }
                }
            }
            
            vec.clear();
            
            if(so==0)break;
        }
        
        cout<<tot<<endl;
    }
}

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

int a[10][5];
int check(int i,int j,int res){
	if(a[i][j] == 0)return 0;
	if(j == 4)return res;
	if(a[i][j] != a[i][j+1])return res;
	if(a[i][j] == a[i][j+1])return check(i,j+1,res+1);
}

int main(){
	int n,x,m;
	int count;
	int score;
	while(1){
		cin >> n;
		if(n == 0)break;
		for(int i = 0;i < 10;i++){
			for(int j = 0;j < 5;j++){
				a[i][j] = 0;
			}
		}
		m = 0;
		for(int i = 10 - n;i < 10;i++){
			for(int j = 0;j < 5;j++){
				cin >> a[i][j];
			}
			m++;
		}

		score = 0;
		while(1){
			/*
		for(int i = 0;i < 10;i++){
			for(int j = 0;j < 5;j++){
				cout << a[i][j] <<" ";
			}
			cout << endl;
		}
			*/
			count = 0;
			m = 0;
			for(int i = 9;m < n;i--){
				for(int j = 0;j < 5;j++){
					x = check(i,j,1);
					if(x > 2){
						count++;
						score += a[i][j] * x; 
						for(int k = j;k < j + x;k++){
							a[i][k] = 0;
						}
						break;
					}
				}
				m++;
			}
			//cout << count <<endl;
			if(count == 0)break;
			for(int k = 0;k < count;k++){
				for(int j = 0;j < 5;j++){
					m = 0;
					for(int i = 9;m < n;i--){
						if(a[i][j] == 0)swap(a[i][j],a[i-1][j]);
						m++;
					}
				}
			}
		}

		cout << score <<endl;
	}
	return 0;
}