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>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
using namespace std;
int n;
void solve() {
	int a[15][7], sum = 0;
	for1(i, n)for1(j, 5) {
		cin >> a[i][j];
		sum += a[i][j];
	}
	bool b = 1;
	while (b) {
		b = 0;
		for1(i, n) {
			if (a[i][2] == a[i][3] && a[i][3] == a[i][4] && a[i][3] != 0) {
				b = 1;
				if (a[i][1] == a[i][3])a[i][1] = 0;
				if (a[i][5] == a[i][3])a[i][5] = 0;
				a[i][2] = 0;
				a[i][3] = 0;
				a[i][4] = 0;
			}
			else if (a[i][1] == a[i][2] && a[i][2] == a[i][3] && a[i][3] != 0) {
				b = 1;
				a[i][1] = 0;
				a[i][2] = 0;
				a[i][3] = 0;
			}
			else if (a[i][5] == a[i][4] && a[i][4] == a[i][3] && a[i][3] != 0) {
				b = 1;
				a[i][5] = 0;
				a[i][4] = 0;
				a[i][3] = 0;
			}
		}
		for1(j, 5) {
			vector<int>v;
			for1(i, n)if (a[i][j])v.push_back(a[i][j]);
			for1(i, n)a[i][j] = (n - v.size() >= i ? 0 : v[i - n + v.size() - 1]);
		}
	}
	for1(i, n)for1(j, 5)sum -= a[i][j];
	cout << sum << endl;
}
signed main() {
	while (cin >> n, n)solve();
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include<vector>
#include<math.h>
#include<bitset>
#include<string>
#include <deque>
#include<queue>
#include<map>
#include<type_traits>

using namespace std;
int A[6][100];
int H;
int Y = 0;
void serch() {
	int t = 1;
	while (t) {
		t = 0;
	
		for (int i = 1; i <= H; i++) {
			int r = 1;
			for (int j = 1; j < 5; j++) {
				
				if (A[j][i] == A[j + 1][i]&&A[j][i]!=-1&&A[j][i]!=0) {
					
					r++;
				}
				else {
					if (r >= 3) {
						for (int R = 0; R < r; R++) {
							
							t++;
							Y += A[j - R][i];
					
							A[j - R][i] = -1;
						}
						break;
					}
					r = 1;
				}
				if (j == 4 && r >= 3) {
					for (int R = 0; R < r; R++) {
						t++;
					
						Y += A[j+1 - R][i];
						A[j+1 - R][i] = -1;
					}
				}
			

			}
		}

		int u = 1;
		while (u) {
			u = 0;
			for (int i = 0; i < H; i++) {
				for (int j = 1; j <= 5; j++) {
				
					if (A[j][H - i] == -1 && A[j][H - i - 1] == -1)u++;
					if (A[j][H - i] == -1) {
						if (A[j][H - i - 1] == 0) {
							A[j][H - i] = 0;
							goto ggo;
						}
						else {
							A[j][H - i] = A[j][H - i - 1];
							A[j][H - i - 1] = -1;
						}
					}
				}
			ggo:;
			}

		}

	}

}

int main() {
	
	cin >> H;
	while (H) {
		for (int i = 0; i < 100; i++){
			for (int j = 0; j < 6; j++) {
				A[j][i]=0;
			}
		}
		for (int i = 1; i <= H; i++) {
			for (int j = 1; j <= 5; j++) {
				cin >> A[j][i];
			}
		}

		serch();
		cout << Y<< endl;
		Y = 0;
		cin >> H;
	}
}

C++: #include <bits/stdc++.h>
#define REP(a,b,c) for(int a=b; a<(int)c; a++)
#define REPD(a,b,c) for(int a=b; a>=(int)c; a--)
#define rep(a,b) REP(a,0,b)
#define repd(a,b) REPD(a,b,0)
typedef long long int ll;
using namespace std;

void show(int data[][5], int H){
  rep(i,H){
    rep(j,5){
      cout << data[i][j];
    }
    cout << endl;
  }  
}

int main(){
  int H;
  while(true){
    cin >> H;
    if(H == 0) break;
    int data[H][5] = {-1};
    rep(i,H) rep(j,5){
      cin >> data[i][j];
    }
    int ans = 0;

    while(true){
      bool finish_flag = true;
      // show(data,H);
      //cout << endl;
      rep(i,H){
        int cnt = 1, idx = 0, eidx = 0;
        REP(j,1,5){
          eidx = j;
          if(data[i][j] == data[i][idx] && data[i][j] != -1){
            cnt++;
          }else{
            if(cnt >= 3){
              break;
            }
            cnt = 1; idx = j;
          }
        }
        if(cnt >= 3){
          finish_flag = false;
          ans += cnt*data[i][idx];
          REP(j,idx,eidx+1){
            data[i][j] = -1;
          }
        }
      }
      //      show(data,H);
      //cout << endl;
      for(int i=H-1; i>=0; i--){
        rep(j,5){
          if(data[i][j] == -1){
            for(int k=i-1; k>=0; k--){
              if(data[k][j] != -1){
                data[i][j] = data[k][j];
                data[k][j] = -1;
                break;
              }
            }
          }
        }
      }
      if(finish_flag) break;
    }
    cout << ans << endl;
  }
  return 0;
}