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<iomanip>
#include<cstdlib>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<map>
#define REP(i, N) for(ll i = 0; i < N; ++i)
#define FOR(i, a, b) for(ll i = a; i < b; ++i)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

int main(void) {
	while(true) {
		ll h;
		cin>>h;
		if(h == 0) break;
		vector<string> s(5);
		REP(i, 5) {
			s[i].resize(h);
		}
		REP(i, h) {
			REP(j, 5) {
				cin>>s[j][h - 1 - i];
			}
		}
		ll score = 0;
		vector<P> majikesu;
		while(true) {
			REP(i, h) {
				ll cnt = 0;
				vector<ll> kesu;
				FOR(j, 1, 5) {
					if(s[j][i] == '0') continue;
					if(s[j][i] == s[j - 1][i]) {
						if(cnt == 0) {
							cnt = 2;
							kesu.pb(j);
							kesu.pb(j - 1);
						} else {
							++cnt;
							kesu.pb(j);
						}
						if(cnt == 3) {
							if(j + 1 < 5 && s[j + 1][i] == s[j][i]) {
								if(j + 2 < 5 && s[j + 2][i] == s[j][i]) {
									kesu.pb(j + 1);
									kesu.pb(j + 2);
								} else {
									kesu.pb(j + 1);
								}
							}
							break;
						}
					} else {
						kesu.resize(0);
						cnt = 0;
					}
				}
				if(kesu.size() >= 3) {
					if(s[kesu[0]][i] != 0) {
						score += (s[kesu[0]][i] - '0') * kesu.size();
						REP(k, kesu.size()) {
							majikesu.pb(P(kesu[k], i));
						}
					}
				}
			}
			if(majikesu.size() == 0) break;
			else {
				REP(j, majikesu.size()) {
					s[majikesu[j].first][majikesu[j].second] = '.';
				}
				REP(j, 5) {
					REP(k, s[j].size()) {
						if(s[j][k] == '.') {
							s[j].erase(s[j].begin() +  k);
							--k;
						}
					}
				}
				REP(j, 5) {
					REP(k, h - s[j].size()) {
						s[j] += "0";
					}
				}
				majikesu.resize(0);
			}
		}
		cout<<score<<endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int H = 10;
const int W = 5;
int h;
int c[H][W];

int main() {
  int i, j, k, l;
  int res;

  while (1) {
    scanf("%d ", &h); 
    if (!h) break;

    for (i=0; i<h; i++)
      for (j=0; j<W; j++)
        scanf("%d ", &(c[i][j]));

    res = 0;
    bool update = true;
    while (update) {
      update = false;
      for (i=h-1; i>=0; i--) {
        for (j=0; j<3; j++) {
          k = 1;
          while (c[i][j]!=0 && j+k<W && c[i][j+k]==c[i][j]) k++;
          if (k >= 3) {
            res += k * c[i][j];
            for (l=j; l<j+k; l++) c[i][l] = 0;
            update = true;
            break;
          }
        }
      }

      for (i=h-2; i>=0; i--)
        for (j=0; j<W; j++) {
          int d = i;
          while (d+1<h && c[d+1][j]==0) d++;
          if (i!=d) {
            c[d][j] = c[i][j];
            c[i][j] = 0;
          }
        }
    }

    printf("%d\n", res);
  }

  return 0;
}

C++: #pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=1000000007;
ll gcd(ll a,ll b) {return b ? gcd(b,a%b):a;}
ll lcm(ll c,ll d){return c/gcd(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int main(){
  while(1){
      int n;
      cin>>n;
      if(n==0)return 0;
      V<V<ll>> a(n,V<ll>(5));
      for(int i=0;i<n;i++)for(int j=0;j<5;j++)cin>>a[i][j];
      ll ans=0;
      while(1){
          bool make=false;
          for(int i=0;i<n;i++){
              ll bfo=a[i][0],cnt=1,ind=0;
              for(int j=1;j<5;j++){
                  if(bfo!=-1&&j!=4&&bfo==a[i][j])cnt++;
                  else{
                      if(bfo!=-1&&bfo==a[i][j])cnt++;
                      if(bfo!=-1&&cnt>=3){
                          make=true;
                        ans+=bfo*cnt;
                        for(int k=ind;k<ind+cnt;k++)a[i][k]=-1;
                      }
                      cnt=1;bfo=a[i][j];
                      ind=j;
                  }
              }
          }
          if(!make)break;
          for(int i=n-1;i>=1;i--){
              for(int j=0;j<5;j++)if(a[i][j]==-1){
                  for(int k=i;k>=0;k--)if(a[k][j]!=-1){
                      swap(a[k][j],a[i][j]);
                      break;
                  }
              }
          }
      }
     cout<<ans<<"\n";
  }
}