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 FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i < (n);i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define SCANF(i) int i;scanf("%d",&i)
using namespace std;
using P = pair<int,int>;
const long long INF = 1LL << 60;
const int IINF=100000000;
const int MOD = (int)1e9 + 7;
typedef long long ll;
//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    //sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
vector<int> dx={1,0,-1,0};vector<int> dy={0,-1,0,1};

signed main(){
    int h;
    while(cin>>h){
        if(h==0)break;
        vector<vector<int>> list(h,vector<int>(5));
        ll ans=0;bool roopflag=true;
        REP(i,h)REP(j,5)cin >> list[i][j];

        while(true){
            roopflag=false;
            REP(i,h){
                REP(j,3){
                    if(list[i][j] != 0 && list[i][j] == list[i][j+1] && list[i][j+1]==list[i][j+2]){
                        roopflag=true;
                        int cnt =3;int num = list[i][j];
                        FOR(k,j+3,5){
                            if(list[i][j]==list[i][k])cnt++;
                            else break;
                        }
                        //cout << cnt << endl;
                        FOR(d,j,j+cnt)list[i][d]=0;
                        ans+=cnt*num;
                    }
                }
            }
            if(!roopflag)break;
            for(int i=h-1;i>0;i--){
                for(int j=0;j<5;j++){
                    if(list[i][j]==0){
                        for(int q=i-1;q>=0;q--){
                            if(list[q][j]!=0){
                                swap(list[i][j],list[q][j]);
                                break;
                            }
                        }
                    }
                }
            }
        }
        printf("%lld\n",ans);
    }
}

C++: #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

const ll mod = 1e9 + 7;

void fall(vvll& mp) {
  auto fallitr = [&](ll x) {
    ll h = mp.size();
    for (ll ii = h - 1; ii >= 1; ii--) {
      for (ll jj = 0; jj < ii; jj++) {
        if (mp[jj + 1][x] == -1 && mp[jj][x] != -1)
          swap(mp[jj+1][x], mp[jj][x]);
      }
    }
    return;
  };
  rep(j, 5) { fallitr(j); }  // j
}

int main() {
  while (true) {
    ll h;
    cin >> h;
    if (h == 0) break;

    vvll mp(h, vll(5));
    rep(i, h) rep(j, 5) cin >> mp[i][j];

    ll ans = 0;
    bool cont;
    do {
      cont = false;
      rep(i, h) {
        rep(j, 3) {
          if (mp[i][j] == -1) continue;
          bool era = mp[i][j] == mp[i][j + 1] && mp[i][j + 1] == mp[i][j + 2];
          if (era) {
            cont = true;
            ll mv = 3;
            ll mc = mp[i][j];
            mp[i][j] = mp[i][j + 1] = mp[i][j + 2] = -1;
            if (j + 3 < 5 && mc == mp[i][j + 3]) {
              mp[i][j + 3] = -1;
              mv++;
              if (j + 4 < 5 && mc == mp[i][j + 4]) {
                mp[i][j + 4] = -1;
                mv++;
              }
            }
            ans += mv * mc;
          }
        }  // j
      }    // i

      // cout << "before fall" << endl;
      // rep(i, h) {
      //   rep(j, 5) printf("%3lld ", mp[i][j]);
      //   cout << endl;
      // }
      if (cont) fall(mp);

      // cout << "after fall" << cont << endl;
      // rep(i, h) {
      //   rep(j, 5) printf("%3lld ", mp[i][j]);
      //   cout << endl;
      // }

    } while (cont);
    cout << ans << endl;
  }

  return 0;
}