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>
using namespace std;

#define W 5
#define DELETE 0

int main(void){
  while(true){
    int H;
    cin >> H;
    if(H == 0){ break; }

    // ?????¢???????????????
    vector<vector<int> > puyo(H, vector<int>(W));
    for(int i = 0; i < H; i++)
      for(int j = 0; j < W; j++)
        cin >> puyo[i][j];

    // ??????????¶????????????§???????????°?????????
    int flag, ans = 0;
    do{
      /*
      printf("\nans: %d\n", ans);
      for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
          printf("[%d]", puyo[i][j]);
        }
        printf("\n");
      }
      */
      
      // ?????????????????°????????£?????????
      flag = false;

      for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
          if(puyo[i][j] == DELETE){ continue;}
          
          // ??????[i][j]?????????????????¨??????
          // ??????????????§????????????????±???????
          int seg;
          for(seg = 1; (j + seg < W) && (puyo[i][j] == puyo[i][j + seg]); seg++);

          // ??£?¶??????????3??\????????£???????¶????
          if(seg >= 3){
            for(int k = 0; k < seg; k++){
              // ?¶??????´??????????????°????????????????????????????????????
              flag = true;
              ans += puyo[i][j + k];
              puyo[i][j + k] = DELETE;
            }
          }
        }
      }

      // DELETE ????????£????????????????????????????????????
      for(int k = 0; k < H; k++){
        for(int i = 1; i < H; i++){
          for(int j = 0; j < W; j++){
            if(puyo[i][j] == DELETE){
              int tmp = puyo[i][j];
              puyo[i][j] = puyo[i - 1][j];
              puyo[i - 1][j] = tmp;
            }
          }
        }
      }
    }while(flag);

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

C++: //#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

// ?????¬???????????¬??????
#pragma region MACRO
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define PED cout << "\n"
#define rep(i,n) for(int i=0; i<(int)n; ++i)
#define REP(i,x,n) for(int i=x; i<(int)n; ++i)
#define repi(i,n) for(int i=0; i<=(int)n; ++i)
#define REPI(i,x,n) for(int i=x; i<=(int)n; ++i)
#define ILP while(true)
#define FOR(i,c) for(__typeof((c).begin())!=(c).begin(); i!=(c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define mp make_pair
#pragma endregion

// ???
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
typedef pair<string, int> psi;
typedef pair<int, string> pis;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long double> vld;
typedef vector<long> vl;
typedef vector<long long> vll;
typedef vector<string> vs;
#pragma endregion

// Effective std
#pragma region ESTD
template<typename C, typename T> int count(C& c, T t) { return count(ALL(c), t); }
template<typename C, typename F> int count_if(C& c, F f) { return count_if(ALL(c), f); }
template<typename C, typename T> void erase(C& c, T t) { remove(ALL(c), t), c.end(); }
template<typename C> void remove(vector<C>& c, unsigned int index) { c.erase(c.begin()+index); }
template<typename C, typename T, typename U> void replace(C& c, T t, U u) { replace(ALL(c), t, u); }
template<typename C, typename F, typename U> void replace_if(C& c, F f, U u) { (ALL(c), f, u); }
template<typename C> void reverse(C& c) { reverse(ALL(c)); }
template<typename C> void sort(C& c) { sort(ALL(c)); }
template<typename C, typename Pred> void sort(C& c, Pred p) { sort(ALL(c), p); }
#pragma endregion

// ?????°
#pragma region CONST_VAL
constexpr int PI = (2*acos(0.0));
constexpr int EPS = (1e-9);
constexpr int MOD = (int)(1e9+7);
constexpr int INF = 100000000;
#pragma endregion

#define int long long

int field[11][6];
int h;

int check() {
    int row = 0;
    int col = 0;
    int len = 0;
    int score = 0;
    for(row=0; row<h; ++row) {
        for(col=0; col<=2; ++col) {
            for(len=1; col+len<=4; ++len) {
                if(field[row][col]!=field[row][col+len]) break;
            }
            if(len<=2) continue;
            score += field[row][col]*len;
            for(int i=0; i<len; ++i) field[row][col+i] = 0;
        }
    }

    return score;
}

void down() {
    int mn = INF;
    for(int col=0; col<5; ++col) {
        for(int mn=1; mn<h; ++mn) {
            for(int row=h-1; row>=mn; --row) {
                if(field[row][col]==0) {
                    field[row][col] = field[row-1][col];
                    field[row-1][col] = 0;
                }
            }
        }
    }
}

signed main()
{
    while(cin>>h, h) {
        for(int i=0; i<h; ++i) {
            for(int j=0; j<5; ++j)
                cin >> field[i][j];
        }

        int score = 0;
        int cs = 0;
        while(true) {
            cs = check();
            if(cs==0) {
                cout << score << endl;
                break;
            }
            score += cs;
            down();
        }
    }
    return 0;
}