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 <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <tuple>
#include <cassert>
#include <exception>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define sz size()
#define print(X) cout << (X) << endl
static const int INF = 1e+9+7;
ll n,m,l;
string s,t;
int d[200010],dp[1010][1010];
double w[1000],v[1000];
double box[200010];
int field[200][200];

int main(){
  while(1){
    cin >> n;
    if(!n)break;
    clr(field,0);
    rep(i,0,n)rep(j,0,5)cin >> field[i][j];
    ll ans = 0;
    bool f = true;
    while(f){
      f = false;
      rep(i,0,n){
        rep(j,0,5){
          l = 1;
          rep(k,j,5){
            if(field[i][k] == 0)break;
            if(field[i][k] != field[i][k+1])break;
            l++;
          }
          if(l >= 3){
            f = true;
            rep(k,j,j+l){
              ans += field[i][k];
              field[i][k] = 0;
            }
          }
        }
      }
      // rep(i,0,n){
      //   rep(j,0,5)cout << field[i][j];
      //   puts("");
      // }
      // puts("");
      pel(i,n-1,0){
        rep(j,0,5){
          if(!field[i][j]){
            // cout << i << " " << j << endl;
            l = 0;
            pel(k,i,0){
              if(!field[k][j])l++;
              else break;
            }
            rep(k,0,n-i){
              if(i - k < 0)break;
              if(i - k - l< 0)break;
              field[i-k][j] = field[i-k-l][j];
              field[i-k-l][j] = 0;
            }
          }
        }
      }
      // rep(i,0,n){
      //   rep(j,0,5)cout << field[i][j];
      //   puts("");
      // }
      // puts("===================================");
    }
    print(ans);
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,s,e) for (int i=s;i<(e);i++)
#define All(v) (v).begin(),(v).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define int long long
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18 * 5;


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int H;
    cin >> H;
    while(H != 0){
        vector< vector<int> > p(7, vector<int>(H));
        REP(i, H) p[0][i] = -1;
        REP(i, H)FOR(j, 1, 6) cin >> p[j][H-1-i];      // 列、行
        REP(i, H) p[6][i] = -1;
        
        int ans = 0;
        int tmp = -1;
        while(tmp != 0){
            tmp = 0;
            REP(i, H){
                int ct = 0, now = 0;
                FOR(j, 1, 7){
                    if(p[now][i] != p[j][i]){
                        if(ct >= 3){
                            tmp += ct*p[now][i];
                            FOR(k, now, j){
                                p[k][i] = -2;
                            }
                        }
                        ct = 1;
                        now = j;
                        
                    }else{
                        ct++;
                    }
                }
            }
            FOR(j, 1, 6){
                p[j].erase(remove(All(p[j]), -2), p[j].end());
                while(p[j].size() != H) p[j].pb(0);
            }
            ans += tmp;
        }
        // REP(i, H){
        //     FOR(j, 1, 6) cout << p[j][H-1-i];
        //     cout << "\n";
        // }
        cout << ans << "\n";
        cin >> H;
    }
    return 0;
}