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 <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

class Solver {
};

int main() {
	while (true) {
		int h;
		cin >> h;
		if (h == 0)break;

		vector<vector<int>> grid(h, vector<int>(6, 0));
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < 5; j++) {
				int val;
				cin >> val;
				grid[i][j] = val;
			}
		}

		int res = 0;
		while (true) {
			bool exit = true;
			for (int i = 0; i < h; i++) {
				int prev = 0;
				int seq = 0;
				for (int j = 0; j < 6; j++) {
					if (prev == grid[i][j]) {
						seq++;
					}
					else {
						if (seq >= 3 && prev > 0) {
							res += prev * seq;
							for (int k = 1; k <= seq; k++) {
								grid[i][j - k] = 0;
							}
							exit = false;
						}
						prev = grid[i][j];
						seq = 1;
					}
				}
			}
			if (exit)break;

			for (int i = 0; i < 5; i++) {
				vector<int>tmp;
				for (int j = h - 1; j >= 0; j--) {
					if (grid[j][i] > 0)tmp.emplace_back(grid[j][i]);
				}
				for (int j = 0; j < h; j++) {
					if (j < tmp.size())grid[h - 1 - j][i] = tmp[j];

					else grid[h - 1 - j][i] = 0;
				}
			}
		}
		cout << res << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
typedef long long ll;
typedef pair<int64_t,int64_t> pll;
int dx[]={1,-1,0,0,1,-1,1,-1};
int dy[]={0,0,1,-1,1,-1,-1,1};
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define ALL(a) begin(a),end(a)
#define sz(s) (s).size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
int n;
int sam[]={7,14,28,15,30,31};

vector<vector<int> >a(11,vector<int>(5));
void reset()
{
    REP(k,5)
    {
        REP(i,n)
        {
            if(a[i][k]==0)
            {
                for(int j=i;j>0;j--)
                {
                    swap(a[j][k],a[j-1][k]);
                }
            }
        }
    }
}


int main()
{
    while(cin>>n,n)
    {
        REP(i,n)REP(j,5)cin>>a[i][j];
        bool flag=1;
        int sum=0;
        while(flag)
        {
            flag=0;
            REP(i,n)
            {
                int now=a[i][0];int times=1;int last;
                rep(j,1,5)
                {
                    if(a[i][j]==a[i][j-1])
                    {
                        times++;
                    }else if(times>=3)
                    {
                        last=j-1;
                        break;
                    }else{
                        now=a[i][j];
                        times=1;
                    }
                    if(j==4)last=j;
                }
                if(times>=3&&now!=0)
                {
                    sum+=times*now;
                    flag=1;
                    for(int k=last;k>last-times;k--)a[i][k]=0;
                    //cout<<"!!"<<last<<"!!"<<endl;
                }
            }
            reset();
            //cout<<"..................."<<endl;REP(i,n){REP(j,5)cout<<a[i][j]<<" ";cout<<endl;}
        }
        cout<<sum<<endl;
    }
}

C++: #line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") { cout << (val ? yes : no) << endl; }

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}


#line 2 "tmp.cpp"

int calcRow(vi &v) {
  REP(i, 3) {
    if (v[i] == -1)
      continue;
    int cnt = 1;
    for (int j = 1; i + j < 5; j++) {
      if (v[i] == v[i + j]) {
        cnt++;
      } else {
        if (cnt >= 3) {
          int ret = cnt * v[i];
          REP(k, j) { v[i + k] = -1; }
          return ret;
        }
        cnt = 0;
        break;
      }
    }
    if (cnt != 0) {
      int ret = cnt * v[i];
      FOR(k, i, 5) v[k] = -1;
      return ret;
    }
  }
  return 0;
}

void calcCol(vector<vi> &cell, int h) {
  REP(j, 5) {
    for (int i = h - 1; i > 0; i--) {
      if (cell[i][j] == -1) {
        for (int above = i - 1; above >= 0; above--) {
          if (cell[above][j] != -1) {
            cell[i][j] = cell[above][j];
            cell[above][j] = -1;
            break;
          }
        }
      }
    }
  }
}

void print(vector<vi> &cell, int h) {
  cout << endl;
  REP(i, h) {
    REP(j, 5) { cout << cell[i][j] << (j == 4 ? "\n" : " "); }
  }
  cout << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int h;
    cin >> h;
    if (h == 0)
      break;
    vector<vi> cell(h, vi(5, 0));
    int ans = 0;
    REP(i, h) {
      REP(j, 5) { cin >> cell[i][j]; }
    }

    bool changed = true;
    while (changed) {
      changed = false;
      REP(i, h) {
        int p = calcRow(cell[i]);
        if (p != 0)
          changed = true;
        ans += p;
      }
      calcCol(cell, h);
    }
    cout << ans << endl;
  }
  return 0;
}