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 <vector>
#include <algorithm>
using namespace std;
int main(){
  int H;
  //?????????????????¶????????\???
  while (cin >> H && H>0) {
    vector< vector<int> > c(5,vector<int>(H));
    int sum = 0;
    for (int i=H-1; i>=0; i--){
      for (int j=0; j<5; j++){
	cin >> c[j][i];
      }
    }
    //????????°??????3?????\????¨???????????????´???????????°????¨??????????0?????????
    int flg = 1; //?¶?????????¨???flg == 1
    while (flg) { 
      flg = 0;
      for (int i=H-1; i>=0; i--){
	for (int j=0; j<3; j++) {
	   int cnt = 1; //?¨???????????????°
	  if (c[j][i] != 0) {
	    int flag = 1;
	    for (int k=j+1; k<5 && flag; k++) { //????¨??????????????????????
	      if (c[j][i] == c[k][i])
		cnt++;
	      if (c[j][i] != c[k][i] || k == 4) { //?????????????????£???????????????????????????
		flag = 0;
		if (cnt >= 3) { //3?????\???????????§?????¨???
		  sum += c[j][i] * cnt; //????????????
		  for (int l=j; l<=j+cnt-1; l++) { //0???????????????
		    c[l][i] = 0;
		  }
		  flg = 1;
		}
	      }
	    }
	  }
	}
      }
      for (int j=0; j<5; j++) {
	remove(c[j].begin(), c[j].end(), 0), c[j].end();
      }
    }
    //???????????????????????????0????????£??????flg==0??¨?????????????????±???
    cout << sum << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
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 (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;
        
    }else{
        return a.second < b.second;
    }
}


bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};



int main() {
    while(1){
    ll H;
    cin >>H;
    if(H==0) break;
    vector<vector<ll>> R(H,vector<ll>(5));

    rep(i,H) rep(j,5) cin>>R[i][j];
    ll ans = 0;
    rep(m,H){
        rep(i,H){
            ll cnt=0;
            ll p=-1;
            rep(j,5){
                if(R[i][j] == p && R[i][j] != -1){
                    cnt++;
                    if(j==4){
                        if(cnt>=3){
                            ans += R[i][j-1]*cnt;
                            rep(k,cnt){
                                R[i][j-k] = -1;
                            }
                        }
                    }
                }else{
                    if(cnt>=3){
                        ans += R[i][j-1]*cnt;
                        rep(k,cnt){
                            R[i][j-k-1] = -1;
                        }
                    }
                    cnt = 1;
                }
                

                p = R[i][j];
            }
        }
        rep(j,5){
            vector<ll> tmp;
            rep(i,H){
                if(R[H-1-i][j] != -1){
                    tmp.pb(R[H-1-i][j]);
                }
            }
            rep(i,H){
                if(i<tmp.size()){
                    R[H-1-i][j] = tmp[i];
                }else{
                    R[H-1-i][j] = -1;
                }
            }
        }

        /*rep(i,H){
            rep(j,5){
                cout << R[i][j]<<" ";
            }
            cout <<endl;
        }*/

    }

    cout << ans<<endl;
    }
    return 0;
    

}