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 _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;

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

// container util
#define all(x) (x).begin(), (x).end()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                         \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

//
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

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

  int H;
  while (cin >> H, H) {
    vector<vector<int>> board(H, vector<int>(5, 0));
    rep(i, H) rep(j, 5) cin >> board[i][j];
    bool end = false;
    int ans = 0;
    while (true) {
      //手順1
      int sum = 0;
      rep(i, H) {
        int pid = 0, cnt = 1;
        FOR(j, 1, 5) {
          if (board[i][pid] == board[i][j])
            cnt++;
          else {
            if (3 <= cnt) break;
            pid = j;
            cnt = 1;
          }
        }
        if (3 <= cnt) {
          sum += cnt * board[i][pid];
          FOR(j, pid, pid + cnt) board[i][j] = 0;
        }
      }

      if (sum == 0) break;
      ans += sum;
      //手順2
      bool end = true;
      while (end) {
        end = false;
        for (int i = H - 1; 0 <= i; i--) {
          rep(j, 5) {
            if (i == 0) continue;
            if (board[i][j] == 0) {
              if (board[i - 1][j] != 0) {
                board[i][j] = board[i - 1][j];
                board[i - 1][j] = 0;
                end = true;
              }
            }
          }
        }
      }
    }
    cout << ans << "\n";
  }

  return 0;
}

C++: #include <cstdio>
#include <cstdint>
#include <vector>
#include <queue>

int nya(std::vector<std::vector<int>>& a) {
  size_t h = a.size();
  int res = 0;
  for (size_t i = 0; i < h; ++i) {
    size_t l = 2;
    size_t r = 2;
    if (a[i][1] == a[i][2]) {
      l = 1;
      if (a[i][0] == a[i][1]) l = 0;
    }
    if (a[i][3] == a[i][2]) {
      r = 3;
      if (a[i][4] == a[i][3]) r = 4;
    }
    if (r-l+1 < 3) continue;

    res += a[i][2] * (r-l+1);
    for (size_t j = l; j <= r; ++j) a[i][j] = 0;
  }

  for (size_t j = 0; j < 5; ++j) {
    std::queue<int> q;
    for (size_t i = h; i--;) {
      if (a[i][j]) q.push(a[i][j]);
      a[i][j] = 0;
    }
    size_t i = h-1;
    while (!q.empty()) {
      int t = q.front();
      q.pop();
      a[i--][j] = t;
    }
  }
  return res;
}

int testcase_ends() {
  size_t n;
  scanf("%zu", &n);
  if (n == 0) return 1;

  std::vector<std::vector<int>> a(n, std::vector<int>(5));
  for (auto& ai: a)
    for (auto& aij: ai)
      scanf("%d", &aij);

  int res = 0;
  while (true) {
    int kieta = nya(a);
    if (!kieta) break;
    res += kieta;
  }
  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

C++: #pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

//#define TEST

void show_b(vector<vector<int>> b) {
#ifdef TEST
	for (int i = 0; i < b.size(); ++i) {
		for (int j = 0; j < 5; ++j) cout << b[i][j] << " ";
		cout << endl;
	}
#endif
}

bool solve() {
	int H; cin >> H;
	if (H == 0) return false;
	vector<vector<int>> b(H, vector<int>(5));
	for (int i = 0; i < H; ++i)
		for (int j = 0; j < 5; ++j)
			cin >> b[i][j];
	bool deleted; int res = 0;
	do {
		vector<vector<int>> newb(H, vector<int>(5, 0));
		deleted = false;
		for (int i = 0; i < H; ++i) {
			for (int j = 0; j < 5; ++j) {
				if (j >= 2 && b[i][j] == b[i][j - 1] && b[i][j - 1] == b[i][j - 2]) {
					if (b[i][j]) deleted = true;
					newb[i][j] = b[i][j];
					for (int jj = j; jj >= j - 2; --jj) {
						res += newb[i][jj];
						newb[i][jj] = 0;
					}
				}
				else newb[i][j] = b[i][j];
			}
		}
		for (int i = H - 1; i > 0; --i) {
			for (int j = 0; j < 5; ++j) {
				if (newb[i][j] == 0) {
					int ptr = i - 1;
					while (ptr >= 0 && newb[ptr][j] == 0) --ptr;
					if (ptr >= 0) {
						newb[i][j] = newb[ptr][j];
						newb[ptr][j] = 0;
					}
				}
			}
		}
		b = newb;
		show_b(b);
	} while (deleted);
	cout << res << endl;
	return true;
}

int main() {
	while (solve());
	return 0;
}