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++: #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 1e18
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define MOD 998244353
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
int main() {
	while (true) {
		int h;
		cin >> h;
		if (h == 0) return 0;
		int gogo[10][5] = {};
		int ans = 0;
		REP(i, h) {
			REP(q, 5) {
				cin >> gogo[(10 - h)+i][q];
			}
		}
		int diff = 1;
		while (diff != 0) {
			diff = 0;
			int deleted[10][5] = {};
			REP(i, 10) {
				for (int q = 0; q + 2 < 5; ++q) {
					if(gogo[i][q] == gogo[i][q+1] && gogo[i][q] == gogo[i][q+2]){
						deleted[i][q] = 1;
						deleted[i][q + 1] = 1;
						deleted[i][q + 2] = 1;
					}
				}
			}
			REP(i, 10) {
				REP(q, 5) {
					if (deleted[i][q] == 1) {
						diff += gogo[i][q];
						gogo[i][q] = 0;
					}
				}
			}
			REP(t,10)
			for (int i = 8; i >= 0; --i) {
				//simulate droppings
				for (int q = 0; q < 5; ++q) {
					if (gogo[i + 1][q] == 0) {
						gogo[i + 1][q] = gogo[i][q];
						gogo[i][q] = 0;
					}
				}
			}
			ans += diff;
		}
		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for(int i=(a);i>=(b);i--)
#define RREP(i, n) RFOR(i, n, 0)
#define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())

typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1 << 30;

template<typename T>
void printv(vector<T> const& s) {
  REP(i, SZ(s)) {
    cout << s[i] << " ";
  }
  cout << endl;
}

class Puzzle {
  vvi g;
  int height;
  bool update;
  vector<vector<bool>> del;
  int score;

public:  
  void init(int h, vvi a) {
    g = a;
    height = h;
    update = false;
    del.resize(h);
    REP(i, h) {
      del[i].resize(5, false);
    }
    score = 0;
  }

  int getScore() {
    return score;
  }
  
  void searchdel() {
    REP(i, height) {
      int cnt = 1;
      REP(j, 5) {
        if(j != 0 && g[i][j] == g[i][j-1] && g[i][j] != 0) {
          cnt++;
          if(cnt == 3) {
            update = true;
            del[i][j-2] = true;
            del[i][j-1] = true;
            del[i][j] = true;
          } else if(cnt > 3) {
            del[i][j] = true;
          }
        }
        else cnt = 1;
      }
    }
  }

  void upd() {
    REP(i, height) {
      REP(j, 5) {
        if(del[i][j]) {
          score += g[i][j];
          g[i][j] = 0;
        }
      }
    }
  }

  bool turnend() {
    REP(i, height) {
      REP(j, 5) {
        del[i][j] = false;
      }
    }

    REP(k, height) {
      RREP(i, height-2) {
        REP(j, 5) {
          if(g[i+1][j] == 0) {
            g[i+1][j] = g[i][j];
            g[i][j] = 0;
          }
        }
      }
    }


    if(!update) return true;
    else {
      update = false;
      return false;
    }
  }
};

int main () {
  cin.tie(0);
  cout << setprecision(10);

  while(1) {
    int h; cin >> h;
    if(h == 0) break;
    vvi a(h);
    REP(i, h) {
      a[i].resize(5);
      REP(j, 5) cin >> a[i][j];
    }
    Puzzle p;
    p.init(h, a);
    while(1) {
      p.searchdel();
      p.upd();
      if(p.turnend()) break;
    }
    cout << p.getScore() << endl;
  }
}