import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

const real eps = 1e-10;

void main(){
	while(true) {
		auto h = readInt();
		if(h == 0) return;

		auto p = new int[][](5, 100);
		for(int i; i < 5; ++i) {
			for(int j; j < 100; ++j) {
				p[i][j] = 10000 + 100*i + j;
			}
		}
		for(int i; i < h; ++i) {
			auto r = readInts();
			for(int j; j < 5; ++j) {
				p[j][h-i-1] = r[j];
			}
		}
		int ans;
		bool f = true;
		while(f) {
			f = false;
			for(int i = h-1; i >= 0; --i) {
				int l;
				int u;
				while(u < 5 && l < 3) {
					if(p[l][i] == p[u][i]) {
						++u;
					} else {
						if(u-l >= 3) {
							ans += p[l][i] * (u-l);
							f = true;
							for(int j = l; j < u; ++j) {
								p[j][i..$-1] = p[j][i+1..$].dup[];
							}
						}
						l = u;
					}
				}
				if(u-l >= 3) {
					ans += p[l][i] * (u-l);
					f = true;
					for(int j = l; j < u; ++j) {
						p[j][i..$-1] = p[j][i+1..$].dup[];
					}
				}
			}
		}
		writeln(ans);
	}
}