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

using ll = long long;
#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define INF 1e9
// constexpr ll INF = 1LL << 60;

int W = 5;
int main(){
  while(true){
    int H;
    cin >> H;
    if(H==0) break;
    vector<vector<int>> A(H, vector<int>(W));
    REP(i, H){
      REP(j, W){
        cin >> A[i][j];
      }
    }
    bool deleted = true;
    int ans = 0;
    while(deleted){
      deleted = false;
      REP(i, H){
        REP(j, W-2){
          if(A[i][j] != -1 && A[i][j] == A[i][j+1]&& A[i][j] == A[i][j+2]){
            deleted = true;
            int x = A[i][j];
            ans += x*3;
            A[i][j] = A[i][j+1] = A[i][j+2] = -1;
            FOR(k, j+3, W){
              if(x==A[i][k]){
                ans+=x;
                A[i][k] = -1;
              }
              else{
                break;
              }
            }
          }
        }
      }
      if(deleted){
        REP(j, W){
          REPR(i, H-2){
            if(A[i][j]==-1) continue;
            int h = i;
            while(h+1 < H && A[h+1][j] == -1){
              A[h+1][j] = A[h][j];
              A[h][j] = -1;
              h++;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int h;
    while(cin >> h, h){
        vector<vector<int>> v(7, vector<int>(h+2, -1));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < 5; ++j) {
                scanf("%d", &v[j+1][i+1]);
            }
        }
        int ok = 1, ans = 0;
        while(ok){
            ok = 0;
            for (int i = 4; i >= 2; --i) {
                for (int j = 0; j < h; ++j) {
                    for (int k = 0; k+i < 5; ++k) {
                        int w = 1;
                        for (int l = k; l < k+i; ++l) {
                            if(v[l+1][j+1] != v[l+2][j+1]) w = 0;
                            if(v[l+1][j+1] == -1) w = 0;
                        }
                        if(w){
                            ok = 1;
                            for (int l = k; l <= k+i; ++l) {
                                ans += v[l+1][j+1];
                                v[l+1][j+1] = -1;
                            }
                            break;
                        }
                    }
                }
            }
            for (int i = 1; i <= 5; ++i) {
                stable_sort(v[i].begin()+1,v[i].end()-1, [&](int x, int y){
                    return (x == -1) > (y == -1);
                });
            }
        }
        cout << ans << "\n";
    }
    return 0;
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
int main(void)
{
  for(;;){
    int h;
    cin >> h;
    if(!h) return 0;
    int field[16][8];
    REP(i,16) REP(j,8) field[i][j]=0;
    while(h--)
      REP(i,5) cin >> field[h][i];
    int answer=0;
    bool flag=true;
    while(flag){
      flag=false;
      REP(i,10){
	if(field[i][2]==0) continue;
	int l=2,r=2;
	while(l>0&&field[i][l-1]==field[i][2]) --l;
	while(r<4&&field[i][r+1]==field[i][2]) ++r;
	if(r-l>=2)
	  for(int j=l;j<=r;++j){
	    answer+=field[i][j];
	    field[i][j]=0;
	    flag=true;
	  }
      }
      bool fall=true;
      while(fall){
	fall=false;
	REP(i,10) REP(j,5)
	  if(field[i+1][j]&&!field[i][j]){
	    field[i][j]=field[i+1][j];
	    field[i+1][j]=0;
	    fall=true;
	  }
      }
    }
    cout << answer << endl;
  }
}