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<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>

using namespace std;

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


const int H = 11;
const int W = 5;
int h;
int board[H][W];

bool input(){
	cin>>h;
	if(h==0)return false;
	
	rep(i,H)rep(j,W)board[i][j]=0;
	rep(i,h){
		rep(j,W)cin>>board[i][j];
	}
	return true;
}


int dela[H][W];

void calc_del(){
	rep(i,H)rep(j,W)dela[i][j]=0;
	rep(i,h){
		rep(j,W-2){
			int num = board[i][j];
			bool hit = true;
			rep(k,3){
				if(board[i][j+k]!=num)hit=false;
			}
			if(hit){
				rep(k,3)dela[i][j+k]=1;
			}
		}
	}
}

int del(){
	int ans = 0;
	rep(i,H){
		rep(j,W){
			if(dela[i][j]==1){
				ans += board[i][j];
				board[i][j]=0;
			}
		}
	}
	return ans;
}

void tume(){
	rep(i,h){
		for(int i=h-1;i>=1;i--){
			rep(j,W){
				if(board[i][j]==0){
					swap(board[i][j], board[i-1][j]);
				}
			}
		}
	}
}

void print(){
	rep(i,h){
		rep(j,W){
			printf("%d ",board[i][j]);
		}puts("");
	}
}

void solve(){
	int ans= 0;
	while(1){
		//print();puts("");
		
		calc_del();
		int val = del();
		ans += val;
		if(val==0)break;
		tume();
		
	}
	cout<<ans<<endl;
}

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

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

int main(){
    int H;
    while(cin >> H && H > 0) {
        int grid[55][55];
        int W = 5;
        REP(y, H) REP(x, W) cin >> grid[y][x];
        int ans = 0;
        REP(_, H * W) {
            bool d[55][55] = {};
            REP(y, H) {
                int c = 1;
                for(int x = 1; x < W; x++) {
                    if(grid[y][x] == grid[y][x - 1]) {
                        c++;
                        if(c >= 3) {
                            REP(i, c) {
                                d[y][x - i] = true;
                            }
                        }
                    } else {
                        c = 1;
                    }
                }
            }
            REP(y, H) REP(x, W) if(d[y][x]) ans += grid[y][x];
            REP(y, H) REP(x, W) if(d[y][x]) grid[y][x] = 0;
            REP(__, H * W) REP(y, H - 1) REP(x, W) if(grid[y][x] > 0 && grid[y + 1][x] == 0) swap(grid[y + 1][x], grid[y][x]);
        }
        cout << ans << endl;
    }
    return 0;
}

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

int step_score=0;

vector< vector<int> > step(vector< vector<int> > T,int H){
    for(int i=0;i<H;i++){
        for(int j=0;j<6;j++){
            for(int k=j+1;k<6;k++){
                if(T.at(i).at(k)!=T.at(i).at(k-1)){
                    if(k-j>=3){
                        for(int l=j;l<k;l++){
                            step_score+=T.at(i).at(l);
                            T.at(i).at(l)=0;
                        }
                    }
                    break;
                }
            }
        }
    }
    vector< vector<int> > T_next(H,vector<int>(6,0));
    for(int j=0;j<6;j++){
        int p=H-1;
        for(int i=H-1;i>=0;i--){
            if(T.at(i).at(j)!=0){
                T_next.at(p).at(j)=T.at(i).at(j);
                p--;
            }
        }
    }
    return T_next;
}

int main(){
    int H;
    cin >> H;
    while(H>0){
        vector< vector<int> > D(H,vector<int>(6,0));
        for(int i=0;i<H;i++) D.at(i).at(5)=10;
        for(int i=0;i<H;i++){
            for(int j=0;j<5;j++){
                cin >> D.at(i).at(j);
            }
        }
        int totalscore=0;
        while(true){
            step_score=0;
            D=step(D,H);
            /*
            for(int i=0;i<H;i++){
                for(int j=0;j<6;j++){
                    cout << D.at(i).at(j) << " ";
                }
                cout << endl;
            }
            cout << endl;
            */
            if(step_score>0){
                totalscore+=step_score;
            }else{
                break;
            }
        }
        cout << totalscore << endl;
        cin >> H;
    }
}