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;
int main(){
  int h;
  while(cin>>h,h){
    int a[10][5]={},ans=0;
    for(int i=0;i<h;i++)
      for(int j=0;j<5;j++)cin>>a[i][j];
    bool f=1;
    while(f){
      f=0;
      for(int i=0;i<h;i++){
	int c=1,p=0;
	for(int j=1;j<5;j++)
	  if(a[i][p]==a[i][j])c++,p=j;
	  else if(c<3)c=1,p=j;
	if(c>=3){
	  ans+=c*a[i][p];
	  for(int j=0;j<c;j++)a[i][p-j]=0;
	}
      }
      for(int i=0;i<5;i++)
	for(int j=1;j<h;j++)
	  for(int k=j;k>0;k--)
	    if(!a[k][i]&&a[k-1][i])swap(a[k][i],a[k-1][i]),f=1;
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define RFOR(i, s, n) for (int i = (n) - 1; i >= (s); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
const long long MOD = 1e9+7, INF = 1e18;
template<class T>inline bool CHMAX(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool CHMIN(T&a,T b){if(a>b){a=b;return true;}return false;}

int N;

int chain(int i,vector<vector<int>>&stone){
	REP(l,5){
		RFOR(r,l+2,5){
			int t = -INF;
			FOR(j,l,r+1){
				if(stone[i][j]==-1)break;
				if(t == -INF)t = stone[i][j];
				else{
					if(t != stone[i][j])break;
				}
				if(j == r){
					FOR(k,l,r+1)stone[i][k] = -1;
					return t * (r+1-l);
				}
			}
		}
	}
	return 0;
}

int solve(){
	vector<vector<int>>stone(N,vector<int>(5));
	REP(i,N)REP(j,5)cin>>stone[i][j];
	int score = -1,ret = 0;
	int loop = 0;
	while(score){
		score = 0;
		REP(i,N)score += chain(i,stone);
		int flg;
		do{
			flg = 0;
			REP(i,N-1)REP(j,5)if(stone[i+1][j] == -1 && stone[i][j] != -1){
				swap(stone[i+1][j],stone[i][j]);
				flg = 1;
			}
		}while(flg);
		ret += score;
		loop++;
	}
	return ret;
}

signed main(){
	vector<int>ans;
	while(cin>>N,N)ans.push_back(solve());
	for(auto e:ans)cout<<e<<endl;
}

C++: #include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <ctype.h>
#include <vector>
using namespace std;

#define ll long long int

int cell[5][10] = {};

bool tune()
{
    int f = false;
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (cell[i][j] == 0)
            {
                for (int k = j; k < 10; k++)
                {
                    if (cell[i][k] != 0)
                    {
                        cell[i][j] = cell[i][k];
                        cell[i][k] = 0;
                        f = true;
                        break;
                    }
                }
            }
        }
    }
    return f;
}

int main()
{
    int h;

    while (cin >> h)
    {
        if (h == 0)
            break;
        int sum = 0;
        for (int i = 0; i < 5; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                cell[i][j] = 0;
            }
        }
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                cin >> cell[j][i];
            }
        }
        for (int i = 0; i < 5; i++)
        {
            reverse(cell[i], cell[i] + h);
        }
        while (1)
        {
            for (int i = 0; i < h; i++)
            {
                bool row[3] = {};
                for (int j = 0; j < 3; j++)
                {
                    if (cell[j][i] == cell[j + 1][i] && cell[j + 2][i] == cell[j + 1][i])
                        row[j] = true;
                }
                for (int j = 0; j < 3; j++)
                {
                    if (row[j])
                    {
                        for (int k = 0; k < 3; k++)
                        {
                            sum += cell[j + k][i];
                            cell[j + k][i] = 0;
                        }
                    }
                }
            }
            if (!tune())
                break;
        }
        cout << sum << endl;
    }
}