import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.regex : regex;
import std.container;

void main()
{
	int h;
	while (h = readln.chomp.to!int, h) {
		auto field = new int[][](h, 5);
		foreach (i; 0..h) {
			field[i] = readln.chomp.split.map!(to!int).array;
		}
		int score;
		while (1) {
			bool f;

			foreach (i; 0..h) {
				foreach (j; 0..4) {
					auto num = field[i][j];
					if (!num) continue;
					auto cnt = 1;
					foreach (k; j+1..5) {
						if (field[i][k] == num) cnt++;
						else break;
					}
					if (cnt >= 3) {
						score += num * cnt;
						f = 1;
						foreach (k; j..j+cnt) {
							field[i][k] = 0;
						}
						break;
					}
				}
			}

			foreach (i; 0..5) {
				foreach (j; 1..h) {
					if (field[j][i]) continue;
					foreach_reverse (k; 1..j+1) {
						field[k][i] = field[k-1][i];
					}
					field[0][i] = 0;
				}
			}
			if (!f) break;
		}
		score.writeln;
	}
}