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;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

int h;
int puzzle[10][30];

void print_p() {
    for(int i = 0; i < 5; ++i) {
        for(int j = 0; j < 20; ++j) {
            cout << puzzle[i][j] << " ";
        }
        cout << endl;
    }
}

bool flg5(int t) {
    bool ch = true;
    for(int i = 1; i < 5; ++i) {
        if(puzzle[i][t] != puzzle[i-1][t]) ch = false;
    }
    if(puzzle[0][t] == -1 || puzzle[0][t] == 0) false;
    return ch;
}

bool flg4(int t) {
    bool ch = false;
    if(puzzle[0][t] == puzzle[1][t] && puzzle[1][t] == puzzle[2][t] && puzzle[2][t] == puzzle[3][t] && puzzle[0][t] != -1 && puzzle[0][t] != 0) ch = true;
    if(puzzle[1][t] == puzzle[2][t] && puzzle[2][t] == puzzle[3][t] && puzzle[3][t] == puzzle[4][t] && puzzle[4][t] != -1 && puzzle[4][t] != 0) ch = true;
    return ch;
}

bool flg3(int t) {
    bool ch = false;
    if(puzzle[0][t] == puzzle[1][t] && puzzle[1][t] == puzzle[2][t] && puzzle[0][t] != -1 && puzzle[0][t] != 0) ch = true;
    if(puzzle[1][t] == puzzle[2][t] && puzzle[2][t] == puzzle[3][t] && puzzle[1][t] != -1 && puzzle[1][t] != 0) ch = true;
    if(puzzle[2][t] == puzzle[3][t] && puzzle[3][t] == puzzle[4][t] && puzzle[2][t] != -1 && puzzle[2][t] != 0) ch = true;
    return ch;
}

int calc_score() {
    // 左から連鎖をチェックする
    int ret = 0;
    for(int j = 0; j < h; ++j) {
        if(flg5(j)) {
            // 5連鎖
            ret += 5*puzzle[0][j];
            for(int i = 0; i < 5; ++i) {
                puzzle[i][j] = -1;
            }
        }else {
            if(flg4(j)) {
                // 4連鎖
                if(puzzle[0][j] == puzzle[1][j]) {
                    ret += 4*puzzle[0][j];
                    for(int i = 0; i < 4; ++i) {
                        puzzle[i][j] = -1;                 
                    }
                }else {
                    ret += 4*puzzle[1][j];
                    for(int i = 1; i < 5; ++i) {
                        puzzle[i][j] = -1;
                    }
                }
            }else {
                if(flg3(j)) {
                    // 3連鎖
                    if(puzzle[0][j] == puzzle[1][j] && puzzle[1][j] == puzzle[2][j]) {
                        ret += 3*puzzle[0][j];
                        for(int i = 0; i < 3; ++i) {
                            puzzle[i][j] = -1;
                        }
                    }else if(puzzle[1][j] == puzzle[2][j] && puzzle[2][j] == puzzle[3][j]) {
                        ret += 3*puzzle[1][j];
                        for(int i = 1; i < 4; ++i) {
                            puzzle[i][j] = -1;
                        }
                    }else {
                        ret += 3*puzzle[2][j];
                        for(int i = 2; i < 5; ++i) {
                            puzzle[i][j] = -1;
                        }
                    }
                }
            }
        }
    }
    return ret;
}

void shift_left() {
    for(int i = 0; i < 5; ++i) {
        for(int j = 0; j < h; ++j) {
            if(puzzle[i][j] == -1) {
                for(int k = 0; ; ++k) {
                    if(puzzle[i][j+k] != -1) {
                        swap(puzzle[i][j], puzzle[i][j+k]);
                        break;
                    }
                }
            }
        }
    }
}

int get_score() {
    int res = 0;
    res += calc_score();
    // ここで穴だらけの2次元配列がもらえるので左に詰める
    shift_left();
    return res;
}

int main() {
    while(1) {
        cin >> h;
        if(h == 0) break;
        memset(puzzle, 0, sizeof(puzzle));
        vector< vector<int> > inpuzzle(5, vector<int>(h, 0));
        for(int j = 0; j < h; ++j) {
            for(int i = 0; i < 5; ++i) {
                cin >> inpuzzle[i][j];
            }
        }
        for(int i = 0; i < 5; ++i) reverse(inpuzzle[i].begin(), inpuzzle[i].end());
        for(int i = 0; i < 5; ++i) {
            for(int j = 0; j < h; ++j) {
                puzzle[i][j] = inpuzzle[i][j];
            }
        }
        // 左を1番下とした2次元配列で用意
        // cout << "map" << endl;
        // print_p();

        // 消滅がなくなるまでループを回す
        int now = -1;
        int sum = 0;
        while(now != 0) {
            now = get_score();
            sum += now;
            // cout << "now " << now << endl;
            // print_p();
        }
        cout << sum << endl;
    }
}