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;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int solve(int H, vector<vector<int>>& b) {
    int retval = 0;
    for(;;) {
        bool update = false;
        rep(i, H) rep(j, 3) if (b[i][j]) {
            int k = 0;
            for (; j + k < 5 && b[i][j + k] == b[i][j]; ++k);
            if (k < 3) continue;
            while (0 <= --k) {
                retval += b[i][j + k];
                b[i][j + k] = 0;
            }
            update = true;
        }
        if (!update) break;
        for (int i = H - 1; i > 0; --i) rep(j, 5) if (!b[i][j]) {
            int k = 1;
            for (; 0 <= i - k && !b[i - k][j]; ++k);
            if (0 <= i - k) swap(b[i][j], b[i - k][j]);
        }
    }
    return retval;
}

int main() {
    for (int H; cin >> H, H;) {
        vector<vector<int>> board(H, vector<int>(5));
        rep(i, H) rep(j, 5) cin >> board[i][j];
        cout << solve(H, board) << endl;
    }
    return 0;
}

C++: #include <iostream>
using namespace std;

int a[10][6];
int flag[10][5];

void revise(int h) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < 5; j++) {
      if (flag[i][j]) a[i][j] = -1;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < 5; j++) {
      if (a[i][j] == -1) {
	for (int k = i; k > 0; k--)
	  a[k][j] = a[k - 1][j];
	a[0][j] = 0;
      }
    }
  }
}

int search(int h) {
  int t = 0;
  for (int i = 0; i < h; i++) {
    int count = 0;
    int fig = 0;
    for (int j = 0; j < 5; j++) {
      if (a[i][j] == a[i][j + 1]) {
	count++;
	if (count == 2) {
	  fig = a[i][j];
	  flag[i][j - 1] = flag[i][j] = flag[i][j + 1] = 1;
	} else if (count > 2) flag[i][j + 1] = 1;
      }
      else if (!fig) count = 0;
      else break;
    }
    if (count >= 2) t += (count + 1) * fig;
  }
  return t;
}

void clear(int h) {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < 5; j++)
      flag[i][j] = 0;
}

int main() {
  for ( ; ; ) {
    int h;
    cin >> h;
    if (h == 0) return 0;

    for (int i = 0; i < h; i++)
      for (int j = 0; j < 5; j++)
	cin >> a[i][j];

    for (int i = 0; i < h; i++)
      a[i][5] = 0;

    int point = 0;
    clear(h);
    for ( ; ; ) {
      int t = search(h);
      if (t == 0) break;
      point += t;
      revise(h);
      clear(h);
    }
    cout << point << endl;
  }
}

C++: #include "bits/stdc++.h"

using namespace std;

using ll = long long;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(x) x.begin(),x.end()
#define REPR(i,n) for(ll i=(n)-1;i>=0;i--)
#define FOR(i,m,n) for(ll i=(m);i<(n);i++)
#define int long long

int H;
int A[20][10];
void solve() {
    int H;cin>>H;
    int W = 5;
    if(H==0)exit(0);
    REP(i,H){
        REP(j,5){
            cin>>A[i][j];
        }
    }
    bool found = true;
    int ans = 0;
    while(found){
        found = false;
        REP(i,H){
            REP(l,W){
                int r = l;
                if(A[i][l]==-1)continue;
                while(A[i][r]==A[i][l]){
                    r++;
                    if(r==W)break;
                }
                if(r-l>=3){
                    found = true;
                    FOR(j,l,r){
                        ans+=A[i][j];
                        A[i][j]=-1;
                    }
                }
            }
        }
        REP(_,H+1){
            REPR(i,H-1){
                REP(j,W){
                    if(A[i+1][j]==-1){
                        A[i+1][j]=A[i][j];
                        A[i][j]=-1;
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
}

signed main() {
	while (true)solve();
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;

#define rep(i,n) for(int i=0; i<n; i++)

int h, point;
int f[11][6];

bool check() {
	bool res = false;
	for(int y=h-1; y>=0; y--) {
		for(int x=2; x<5; x++) {
			if(f[y][x]!=0 && f[y][x-2]==f[y][x] && f[y][x-1]==f[y][x]) {
				res = true;
				int dx=1;
				while(x+dx<5 && f[y][x]==f[y][x+dx]) dx++;
				point += f[y][x] * (3+dx-1);
				for(int ex=x-2; ex<x+dx; ex++) {
					f[y][ex] = 0;
				}
			}
		}
	}
	return res;
}

void fall() {
	int py[5] = {};
	rep(y, h) {
		rep(x, 5) {
			f[py[x]][x] = f[y][x];
			if(f[y][x]>0) {
				py[x]++;
			}
		}
	}
	rep(x, 5) {
		for(int y=py[x]; y<h; y++) {
			f[y][x] = 0;
		}
	}
}

int main() {
	while(cin >> h, h) {
		point = 0;
		memset( f, 0, sizeof(f) );
		rep(y, h) {
			rep(x, 5) {
				int k;
				cin >> k;
				f[h-y-1][x] = k;
			}
		}
		while(check()) {
			fall();
		}
		cout << point << endl;
	}
	return 0;
}