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 <list>
#include <vector>

using namespace std;

int main()
{
    while (true) {
        int h;
        scanf("%d", &h);
        if (h == 0) {
            break;
        }

        vector<vector<int>> v;
        v.resize(h);
        for (auto&& r : v) {
            r.resize(5);
            for (auto i = 0; i < 5; ++i) {
                int x;
                scanf("%d", &x);
                r[i] = x;
            }
        }

        int p = 0;
        bool change = false;
        do {
            // printf("debug\n");
            // for (auto&& r : v) {
            //    for (auto&& i : r) {
            //        printf("%2d ", i);
            //    }
            //    printf("\n");
            //}
            change = false;
            for (auto&& r : v) {
                // 5
                if (r[0] > 0 && r[0] == r[1] && r[1] == r[2] && r[2] == r[3] &&
                    r[3] == r[4]) {
                    p += r[0] * 5;
                    r[0] = r[1] = r[2] = r[3] = r[4] = -1;
                    change = true;
                }
                // 4
                if (r[0] > 0 && r[0] == r[1] && r[1] == r[2] && r[2] == r[3]) {
                    p += r[0] * 4;
                    r[0] = r[1] = r[2] = r[3] = -1;
                    change = true;
                }
                if (r[1] > 0 && r[1] == r[2] && r[2] == r[3] && r[3] == r[4]) {
                    p += r[1] * 4;
                    r[1] = r[2] = r[3] = r[4] = -1;
                    change = true;
                }
                // 3
                if (r[0] > 0 && r[0] == r[1] && r[1] == r[2]) {
                    p += r[0] * 3;
                    r[0] = r[1] = r[2] = -1;
                    change = true;
                }
                if (r[1] > 0 && r[1] == r[2] && r[2] == r[3]) {
                    p += r[1] * 3;
                    r[1] = r[2] = r[3] = -1;
                    change = true;
                }
                if (r[2] > 0 && r[2] == r[3] && r[3] == r[4]) {
                    p += r[2] * 3;
                    r[2] = r[3] = r[4] = -1;
                    change = true;
                }
            }

            for (int i = 0; i < 5; ++i) {
                vector<int> t;
                for (size_t j = 0; j < v.size(); ++j) {
                    if (v[j][i] > 0) {
                        t.push_back(v[j][i]);
                    }
                }
                // printf("t size %d\n", t.size());
                // for (auto&& s : t) {
                //    printf("%d ", s);
                //}
                // putchar('\n');
                for (size_t j = 0; j < t.size(); ++j) {
                    v[h - t.size() + j][i] = t[j];
                }
                for (size_t j = 0; j < h - t.size(); ++j) {
                    v[j][i] = -1;
                }
            }
        } while (change);
        printf("%d\n", p);
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end

void solve(){
    int H;
    while((cin>>H,H)){
        int A[10][5];
        rep(i,H)rep(j,5)cin>>A[i][j];
        bool update=1;
        int res=0;
        while(update){
            update=0;
            bool rm[10][5];rep(i,10)rep(j,5)rm[i][j]=0;
            rep(i,H){
                int ct=1;
                for(int j=1;j<5;j++)
                    if(A[i][j]==A[i][j-1])ct++;
                    else{
                        if(ct>2){
                            for(int k=j-ct;k<j;k++){rm[i][k]=1;update|=A[i][k]>0;res+=A[i][k];A[i][k]=0;}
                        }
                        ct=1;
                    }
                if(ct>2){
                            for(int k=5-ct;k<5;k++){rm[i][k]=1;update|=A[i][k]>0;res+=A[i][k];A[i][k]=0;}
                }
            }
            rep(i,5){
                int now=H-1,id=H-1;
                while(id>=0){
                    while(id>=0&&rm[id][i])id--;

                    if(id>=0)
                        {swap(A[now][i],A[id][i]);now--;id--;}
                }
            }
        }
        cout<<res<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}