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>
#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 debug(x) cout << "debug " << x << endl;
const int INF = 100000000;
using namespace std;

int m[11][6];

bool downStone(int x, int y){
    if(m[y][x] == -1) return 0;
    if(m[y + 1][x] == -1){
        m[y + 1][x] = m[y][x];
        m[y][x] = -1;
        return 1;
    }
    return 0;
}

bool deleteLine(int x, int y){
    int c = m[y][x];
    if(c == -1) return 0;
    int cnt = 0;
    rep(i,5 - x){
        if(c == m[y][x + i]) cnt++;
        else break;
    }
    if(cnt >= 3){
        rep(i,cnt){
            m[y][x + i] = -1;
        }
        return 1;
    }
    return 0;
}

int main(){
    int n;
    while(cin >> n, n){
        map<int, int> p;
        rep(i,n){
            rep(j,5){
                cin >> m[i][j];
                p[m[i][j]]++;
            }
        }
        rep(j,1000){
            bool a;
            rep(i,n){
                rep(j,5){
                    deleteLine(j, i);
                }
            }
            while(1){
                if(n == 1) break;
                a = false;
                for(int i = n - 2; i >= 0; i--){
                    rep(j,5){
                        if(downStone(j, i)) a = true;
                    }
                }
                if(not a) break;
            }
        }
        rep(i,n){
            rep(j,5){
                if(m[i][j] != -1){
                    p[m[i][j]]--;
                }
            }
        }
        int sum = 0;
        for(auto it:p){
            sum+=it.first * it.second;
        }
        cout << sum << endl;
        //rep(i,n){ rep(j,5){ cout << m[i][j]; } cout << endl; }
    }
}

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

int main(){
  int sum,a,b,c,d,e,h,k;
  vector<int> ve[6];
  while(1){
    cin>>h;
    if(h==0)break;
    ve[1].clear();
    ve[2].clear();
    ve[3].clear();
    ve[4].clear();
    ve[5].clear();
    for(int i=1;i<=h;i++){
      cin>>a>>b>>c>>d>>e;
      ve[1].push_back(a);
      ve[2].push_back(b);
      ve[3].push_back(c);
      ve[4].push_back(d);
      ve[5].push_back(e);
    }
    reverse(ve[1].begin(),ve[1].end());
    reverse(ve[2].begin(),ve[2].end());
    reverse(ve[3].begin(),ve[3].end());
    reverse(ve[4].begin(),ve[4].end());
    reverse(ve[5].begin(),ve[5].end());
    for(int i=0;i<h;i++){
      ve[1].push_back(-1);
      ve[2].push_back(-1);
      ve[3].push_back(-1);
      ve[4].push_back(-1);
      ve[5].push_back(-1);
    }
    //for(int i=0;i<h;i++)cout<<ve[1][i];
    sum=0;
    while(1){
      k=0;
      for(int i=h-1;i>=0;i--){
	if(ve[1][i]==ve[2][i]&&ve[1][i]==ve[3][i]&&ve[1][i]==ve[4][i]&&ve[1][i]==ve[5][i]&&ve[3][i]!=-1){
	  sum+=ve[3][i]*5;
	  ve[1].erase(ve[1].begin()+i);
	  ve[2].erase(ve[2].begin()+i);
	  ve[3].erase(ve[3].begin()+i);
	  ve[4].erase(ve[4].begin()+i);
	  ve[5].erase(ve[5].begin()+i);
	  k++;
	}else if(ve[1][i]==ve[2][i]&&ve[1][i]==ve[3][i]&&ve[1][i]==ve[4][i]&&ve[3][i]!=-1){
	  sum+=ve[3][i]*4;
	  ve[1].erase(ve[1].begin()+i);
	  ve[2].erase(ve[2].begin()+i);
	  ve[3].erase(ve[3].begin()+i);
	  ve[4].erase(ve[4].begin()+i);
	  k++;
	}else if(ve[2][i]==ve[3][i]&&ve[2][i]==ve[4][i]&&ve[2][i]==ve[5][i]&&ve[3][i]!=-1){
	  sum+=ve[3][i]*4;
	  ve[2].erase(ve[2].begin()+i);
	  ve[3].erase(ve[3].begin()+i);
	  ve[4].erase(ve[4].begin()+i);
	  ve[5].erase(ve[5].begin()+i);
	  k++;
	}else if(ve[1][i]==ve[2][i]&&ve[1][i]==ve[3][i]&&ve[3][i]!=-1){
	  sum+=ve[3][i]*3;
	  ve[1].erase(ve[1].begin()+i);
	  ve[2].erase(ve[2].begin()+i);
	  ve[3].erase(ve[3].begin()+i);
	  k++;
	}else if(ve[2][i]==ve[3][i]&&ve[2][i]==ve[4][i]&&ve[3][i]!=-1){
	  sum+=ve[3][i]*3;
	  ve[2].erase(ve[2].begin()+i);
	  ve[3].erase(ve[3].begin()+i);
	  ve[4].erase(ve[4].begin()+i);
	  k++;
	}else if(ve[3][i]==ve[4][i]&&ve[3][i]==ve[5][i]&&ve[3][i]!=-1){
	  sum+=ve[3][i]*3;
	  ve[3].erase(ve[3].begin()+i);
	  ve[4].erase(ve[4].begin()+i);
	  ve[5].erase(ve[5].begin()+i);
	  k++;
	}
      }
      if(k==0)break;
    }
    cout<<sum<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define rep(i,a) for(int i=0;i<(a);++i)
#define rrep(i,a) for(int i=(a)-1;i>=0;--i)

using namespace std;

int main() {
	int n;
	while(cin >> n,n){
		vector<vector<int> > v(5,vector<int>(n));
		rrep(i,n) rep(j,5) cin >> v[j][i];
		int sum=0,lsum;
		do{
			lsum = sum;
			rep(i,n){
				bool rm[5];
				memset(rm,false,sizeof(rm));
				rep(j,3){
					if(v[j][i] && v[j][i] == v[j+1][i] && v[j+1][i] == v[j+2][i]){
						rm[j]=rm[j+1]=rm[j+2]=true;
					}
				}
				rep(j,5){
					if(rm[j]){
						sum+=v[j][i];
						v[j][i]=0;
					}
				}
			}
			rep(i,5){
				fill(remove(v[i].begin(),v[i].end(),0),v[i].end(),0);
			}
		}while(lsum!=sum);
		cout << sum << endl;
	}
	return 0;
}