Problem:
Chain Disappearance Puzzle
We are playing a puzzle.
An upright board with
H
rows by 5 columns of cells, as shown in 
the figure below, is used in this puzzle.
A stone engraved with a digit, one of 1 through 9, is placed in each of 
the cells.
When three or more stones in horizontally adjacent cells are engraved 
with the same digit, the stones will disappear.
If there are stones in the cells above the cell with a disappeared 
stone, the stones in the above cells will drop down, filling the 
vacancy.
The puzzle proceeds taking the following steps.
When three or more stones in horizontally adjacent cells are 
engraved with the same digit, the stones will disappear. Disappearances 
of all such groups of stones take place simultaneously.
When stones are in the cells above the emptied cells, these stones drop down so that the emptied cells are filled.
After the completion of all stone drops, if one or more groups of 
stones satisfy the disappearance condition, repeat by returning to the 
step 1.
The score of this puzzle is the sum of the digits on the disappeared stones.
Write a program that calculates the score of given configurations of stones.
Input
The input consists of multiple datasets.
Each dataset is formed as follows.
Board height
H
Stone placement of the row 1
Stone placement of the row 2
...
Stone placement of the row
H
The first line specifies the height (
H
) of the puzzle board (1 ≤
H
≤ 10).
The remaining
H
lines give placement of stones on each of the rows from top to bottom.
The placements are given by five digits (1 through 9), separated by a space.
These digits are engraved on the five stones in the corresponding row, in the same order.
The input ends with a line with a single zero.
Output
For each dataset, output the score in a line.
Output lines may not include any characters except the digits expressing the scores.
Sample Input
1
6 9 9 9 9
5
5 9 5 5 9
5 5 6 9 9
4 6 3 6 9
3 3 2 9 9
2 2 1 1 1
10
3 5 6 5 6
2 2 2 8 3
6 2 5 9 2
7 7 7 6 1
4 6 6 4 9
8 9 1 1 8
5 6 1 8 1
6 8 2 1 2
9 6 3 3 5
5 3 8 8 8
5
1 2 3 4 5
6 7 8 9 1
2 3 4 5 6
7 8 9 1 2
3 4 5 6 7
3
2 2 8 7 4
6 5 7 7 7
8 8 9 9 9
0
Output for the Sample Input
36
38
99
0
72

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 1'000'000'007;
ll inf = (1LL << 62);

// clang-format off
auto getll(){ll n; cin >> n; return n;}
auto getstr(){string s; cin >> s; return s;}
auto getdb(){double n; cin >> n; return n;}
auto getvll(ll n){vector<ll> a(n); for (ll i = 0; i < n; ++i) a[i] = getll(); return a;}
auto getvstr(ll n){vector<string> a(n); for (ll i = 0; i < n; ++i) a[i] = getstr(); return a;}
auto getvdb(ll n){vector<double> a(n); for (ll i = 0; i < n; ++i) a[i] = getdb(); return a;}
// clang-format on

pair<ll, ll> func(vector<ll>& a) {
  vector<pair<ll, ll>> tmp;
  ll curr = -1;
  for (ll i = 0; i < a.size(); ++i) {
    if (curr == -1 || a[i] != curr) {
      tmp.push_back({a[i], 1});
      curr = a[i];
    } else {
      tmp.back().second += 1;
      // curr = a[i];
    }
  }
  ll index = 0;
  for (auto& x : tmp) {
    if (x.first != 0 && x.second >= 3)
      return {index, x.second};
    index += x.second;
  }
  return {-1, -1};
}

int main() {
  vector<ll> ans;
  while (true) {
    ll h;
    cin >> h;
    if (h == 0)
      break;
    ll pt = 0;
    vector<vector<ll>> p(h, vector<ll>(5));
    for (ll i = 0; i < h; ++i) {
      for (ll j = 0; j < 5; ++j) {
        cin >> p[i][j];
      }
    }
    for (ll cnt = 0; cnt < h * 5 / 3 + 1; ++cnt) {
      for (ll i = 0; i < h; ++i) {
        auto cv = func(p[i]);
        if (cv.first != -1) {
          pt += p[i][cv.first] * cv.second;
          for (ll k = 0; k < cv.second; ++k) {
            p[i][cv.first + k] = 0;
          }
        }
      }
      for (ll i = 0; i < 5; ++i) {
        vector<ll> index(5, h - 1);
        for (ll j = h - 1; j >= 0; --j) {
          if (p[j][i] != 0) {
            p[index[i]][i] = p[j][i];
            index[i]--;
          }
        }
        for (ll j = index[i]; j >= 0; --j) {
          p[j][i] = 0;
        }
      }
    }
    ans.push_back(pt);
  }
  for (auto& x : ans)
    cout << x << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3460
final_rank: 9

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

bool check(vector<int> X, int L, int R) {
    bool check = true;
    rep(i,L,R-1) {
        if (X[i] != X[i+1]) check = false;
    }
    return check;
}

void refill(vector<int>& X, int L, int R) {
    rep(i,L,R) X[i] = 0;
    return;
}

void Solve(vector<vector<int>>& A, int N, int& ANS) {
    rep(i,0,N) {
        if (check(A[i],0,5)) ANS += A[i][0] * 5, refill(A[i],0,5);
        else if (check(A[i],0,4)) ANS += A[i][0] * 4, refill(A[i],0,4);
        else if (check(A[i],1,5)) ANS += A[i][1] * 4, refill(A[i],1,5);
        else if (check(A[i],0,3)) ANS += A[i][0] * 3, refill(A[i],0,3);
        else if (check(A[i],1,4)) ANS += A[i][1] * 3, refill(A[i],1,4);
        else if (check(A[i],2,5)) ANS += A[i][2] * 3, refill(A[i],2,5);
    }
    vector<vector<int>> X(5);
    rep(i,0,N) {
        rep(j,0,5) {
            if (A[i][j] != 0) X[j].push_back(A[i][j]);
        }
    }
    rep(j,0,5) {
        rep(k,0,(int)(X[j].size())) A[k][j] = X[j][k];
        rep(k,(int)(X[j].size()),N) A[k][j] = 0;
    }
    return;
}

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector A(N,vector<int>(5));
    rrep(i,0,N) {
        rep(j,0,5) cin >> A[i][j];
    }
    int ANS = 0;
    rep(i,0,100) {
        Solve(A,N,ANS);
    }
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 70
memory_kb: 3432
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)

int solve(int n){
  vector<vector<int>> board(n,vector(5,0));
  rep(i,n) rep(j,5) cin>>board[i][j];
  int score=0;
  rep(_,5*n){
    rep(i,n){
      set<int> set;
      rep(j,3) if(board[i][j]==board[i][j+1] && board[i][j+1]==board[i][j+2]){
        set.insert(j);
        set.insert(j+1);
        set.insert(j+2);
      }
      for(int idx:set){
        score+=board[i][idx];
        board[i][idx]=0;
      }
    }
    rep(__,n) rep(i,n-1){
      rep(j,5) if(board[i+1][j]==0) swap(board[i][j], board[i+1][j]);
    }
  }
  return score;
}

int main(){
  for(;;){
    int n; cin>>n;
    if(n==0) return 0;
    cout<<solve(n)<<endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3380
final_rank: 7

code_snippet: #include <iostream>
#include <array>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(x) x.begin(), x.end()

int main() {

    array<array<int,5>,10> x{};
    int h;

    while (true) {
        cin >> h;
        if (h == 0) break;
        rep(i,h) rep(j,5) cin>>x[i][j];

        int score = 0;
        rep(_,500) {
            rep(i,h) {
                if (x[i][0] == x[i][1] && x[i][1] == x[i][2] && x[i][2] == x[i][3] && x[i][3] == x[i][4] && x[i][0] >= 1) {
                    score += x[i][0] * 5;
                    x[i][0] = 0;
                    x[i][1] = 0;
                    x[i][2] = 0;
                    x[i][3] = 0;
                    x[i][4] = 0;
                }
            }
            rep(i,h) rep(j,2) {
                if (x[i][j] == x[i][j+1] && x[i][j+1] == x[i][j+2] && x[i][j+2] == x[i][j+3] && x[i][j] >= 1) {
                    score += x[i][j] * 4;
                    x[i][j]   = 0;
                    x[i][j+1] = 0;
                    x[i][j+2] = 0;
                    x[i][j+3] = 0;
                }
            }
            rep(i,h) rep(j,3) {
                if (x[i][j] == x[i][j+1] && x[i][j+1] == x[i][j+2] && x[i][j] >= 1) {
                    score += x[i][j] * 3;
                    x[i][j]   = 0;
                    x[i][j+1] = 0;
                    x[i][j+2] = 0;
                }
            }
            rep(_,500) {
                for (int i = 1; i < h; i++) rep(j,5) {
                    if (x[i][j] == 0) {
                        x[i][j] = x[i-1][j];
                        x[i-1][j] = 0;
                    }
                }
            }

        }
        cout << score << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 3100
memory_kb: 3352
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T, vector<T>, greater<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;

void solve(int n) {
  vector<vector<int>> a(n, vector<int>(5));
  rep(i,0,n) rep(j,0,5) cin >> a[i][j];
  reverse(all(a));
  int step = 20;
  int ans = 0;
  while(step--) {
    rep(i,0,n) {
      rrep(l,6,3) {
        rep(j,0,6-l) {
          int x = 10, y = -1;
          rep(k,0,l) {
            x = min(x, a[i][j+k]);
            y = max(y, a[i][j+k]);
          }
          if (x == y && y > 0) {
            ans += x * l;
            rep(k,0,l) {
              a[i][j+k] = 0;
            }
          }
        }
      }
    }
    rep(j,0,5) {
      vector<int> b;
      rep(i,0,n) {
        if (a[i][j] != 0) {
          b.eb(a[i][j]);
        }
      }
      rep(i,0,len(b)) {
        a[i][j] = b[i];
      }
      rep(i,len(b),n) {
        a[i][j] = 0;
      }
    }
  }
  cout << ans << endl;
}

int main() {
  int n;
  while(1) {
    cin >> n;
    if (n == 0) break;
    solve(n);
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3328
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
  int H;
  char mas[10][5];
  while (cin >> H, H) {
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < 5; j++) {
        cin >> mas[i][j];
      }
    }
    int ret = 0;
    bool changed = true;
    for (int foo = 0; foo < 100; foo++) {
      for (int q = 0; q < 30; q++) {
        for (int i = H - 1; i > 0; i--) {
          for (int j = 0; j < 5; j++) {
            if (mas[i][j] == '*') {
              swap(mas[i][j], mas[i - 1][j]);
            }
          }
        }
      }
      for (int i = 0; i < H; i++) {
        char prev = mas[i][0];
        int cnt = 0;
        for (int j = 0; j < 5; j++) {
          if (mas[i][j] == '*') {
            cnt = 0;
            continue;
          }
          if (mas[i][j] != prev) {
            prev = mas[i][j];
            cnt = 0;
          }
          cnt++;
          if (cnt == 3) {
            ret += (mas[i][j] - '0') * 3;
            for (int k = j, l = 0; l < cnt; l++, k--) {
              mas[i][k] = '*';
            }
          } else if (cnt > 3) {
            ret += (mas[i][j] - '0');
            mas[i][j] = '*';
          }
        }
      }
    }
    cout << ret << endl;
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3084
final_rank: 2

code_snippet: #include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int res;
vector<string> input(int n) {
  vector<string> res;
  for (int i = 0; i < n; i++) {
    string line;
    for (int j = 0; j < 5; j++) {
      char c;
      cin >> c;
      line += c;
    }
    res.push_back(line);
    line = "";
  }
  return res;
}
pair<string, int> chek(string s) {
  int res = 0;
  string nu = "00000";
  if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3] && s[3] == s[4] &&
      s[0] != '0') {
    return make_pair(nu, (s[0] - '0') * 5);
  }
  if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3] && s[0] != '0') {
    return make_pair(nu.substr(0, 4) + s.substr(4, 1), (s[0] - '0') * 4);
  }
  if (s[1] == s[2] && s[2] == s[3] && s[3] == s[4] && s[1] != '0') {
    return make_pair(s.substr(0, 1) + nu.substr(0, 4), (s[1] - '0') * 4);
  }
  if (s[0] == s[1] && s[1] == s[2] && s[0] != '0') {
    return make_pair(nu.substr(0, 3) + s.substr(3, 2), (s[0] - '0') * 3);
  }
  if (s[1] == s[2] && s[2] == s[3] && s[1] != '0') {
    return make_pair(s.substr(0, 1) + nu.substr(0, 3) + s.substr(4, 1),
                     (s[1] - '0') * 3);
  }
  if (s[2] == s[3] && s[3] == s[4] && s[2] != '0') {
    return make_pair(s.substr(0, 2) + nu.substr(0, 3), (s[2] - '0') * 3);
  }
  return make_pair(s, 0);
}
vector<string> move(vector<string> line) {
  for (int i = line.size() - 2; i >= 0; i--) {
    for (int j = 0; j < 5; j++) {
      if (line[i + 1][j] == '0') {
        line[i + 1][j] = line[i][j];
        line[i][j] = '0';
      }
    }
  }
  return line;
}
int main(void) {
  int n;
  while (cin >> n, n) {
    res = 0;
    vector<string> in = input(n);
    if (in.size() == 1) {
      pair<string, int> line = chek(in[0]);
      cout << line.second << endl;
    } else {
      bool con = true;
      int ll = 0;
      while (con) {
        con = false;
        for (int i = 0; i < in.size(); i++) {
          pair<string, int> line = chek(in[i]);
          in[i] = line.first;
          res += line.second;
          if (line.second)
            con = true;
          in = move(in);
        }
      }
      cout << res << endl;
    }
  }
}
accuracy: 1
time_ms: 10
memory_kb: 3168
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {

    while (true) {

        int h;
        cin >> h;
        if (h == 0) break;
        vector<vector<int> > x(2000, vector<int>(5, 0));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < 5; j++) {
                cin >> x[i][j];
            }
        }

        int score = 0;
        for (int i = 0; i < 500; i++) {
            for (int j = 0; j < h; j++) {
                if (x[j][0] == x[j][1] && x[j][1] == x[j][2] && x[j][2] == x[j][3] && x[j][3] == x[j][4] && x[j][0] >= 1) {
                    score += x[j][0] * 5;
                    x[j][0] = 0;
                    x[j][1] = 0;
                    x[j][2] = 0;
                    x[j][3] = 0;
                    x[j][4] = 0;
                }
            }
            for (int j = 0; j < h; j++) {
                for (int k = 0; k < 2; k++) {
                    if (x[j][k + 0] == x[j][k + 1] && x[j][k + 1] == x[j][k + 2] && x[j][k + 2] == x[j][k + 3] && x[j][k + 0] >= 1) {
                        score += x[j][k + 0] * 4;
                        x[j][k + 0] = 0;
                        x[j][k + 1] = 0;
                        x[j][k + 2] = 0;
                        x[j][k + 3] = 0;
                    }
                }
            }
            for (int j = 0; j < h; j++) {
                for (int k = 0; k < 3; k++) {
                    if (x[j][k + 0] == x[j][k + 1] && x[j][k + 1] == x[j][k + 2] && x[j][k + 0] >= 1) {
                        score += x[j][k + 0] * 3;
                        x[j][k + 0] = 0;
                        x[j][k + 1] = 0;
                        x[j][k + 2] = 0;
                    }
                }
            }
            for (int j = 0; j < 500; j++) {
                for (int j = 1; j < h; j++) {
                    for (int k = 0; k < 5; k++) {
                        if (x[j][k] == 0) {
                        x[j][k] = x[j - 1][k];
                        x[j - 1][k] = 0;
                        }
                    }
                }
            }
        }
        cout << score << endl;
    }
    return (0);
}
accuracy: 1
time_ms: 5040
memory_kb: 3220
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void remake(vector<int>& vec){
    vector<int> vec2;
    for(int i = 0 ; i < vec.size() ; i++){
        if(vec[i]>=0){
            vec2.push_back(vec[i]);
        }
    }
    while(vec2.size()<vec.size()){
        vec2.push_back(-1);
    }
    for(int i = 0 ; i < vec.size() ; i++){
        vec[i] = vec2[i];
    }
}

int solve(int H){
    int cnt = 0;
    vector vec(7,vector<int>(H,-1));
    for(int i = 0 ; i < H ; i++){
        for(int j = 1 ; j <= 5 ; j++){
            cin >> vec[j][i];
        }
    }
    for(int j = 1 ; j <= 5 ; j++){
        reverse(vec[j].begin(),vec[j].end());
    }
    for(int i = 0 ; i <= 18 ; i++){
        for(int h = 0 ; h < H ; h++){
            for(int j = 5 ; j >= 3 ; j--){
                for(int st = 1 ; st <= 5-j+1 ; st++){
                    int en = st+j-1;
                    int pre = vec[st][h];
                    if(pre == -1)continue;
                    bool ok = true;
                    for(int a = st+1 ; a <= en ; a++){
                        if(pre == -1){
                            ok = false;
                        }
                        if(pre != vec[a][h]){
                            ok = false;
                        }
                    }
                    if(ok){
                        for(int a = st ; a <= en ; a++){
                            vec[a][h] = -1;
                        }
                        cnt += j*pre;
                        break;
                    }
                }
            }
        }
        for(int j = 1 ; j <= 5 ; j++){
            remake(vec[j]);
        }
        /*for(int h = 0 ; h < H ; h++){
            for(int j = 1 ; j <= 5 ; j++){
            cout << vec[j][h] << " ";
            }
            cout << endl;
        }*/
    }
    return cnt;
}

int main(){
    while(1){
        int H;
        cin >> H;
        if(H == 0)break;
        cout << solve(H) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3332
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
template<class T> void my_printv(vector<T> v,bool endline = true){
  if(!v.empty()){ for(size_t i{}; i<v.size()-1; i++) cout<<v[i]<<' '; cout<<v.back(); }
  if(endline) cout<<endl;
}
using ll = long long;

const int inf = INT_MAX / 2; const ll INF = 1LL << 60;
const ll MOD = 1000000007;

////////////////////////////////////////////////////////////////////////////////////////////


int main() {
  
  int n;
  while(cin>>n, n) {
    vector<vector<int>> t(n, vector<int> (5));
    for(auto&i:t) for(auto&j:i) cin>>j;
    int ans = 0;
    rep(_, 50*50) {
      rep(i, n) {
        int cnt = 0;
        rep(j, 5) {
          if(j == 0 or t[i][j] == t[i][j-1]) cnt++;
          else {
            if(cnt >= 3) {
              ans += cnt * t[i][j-1];
              rep(k, cnt) t[i][j-1-k] = 0;
            }
            cnt = 1;
          }
        }
        if(cnt >= 3) {
          ans += cnt * t[i][4];
          rep(k, cnt) t[i][4-k] = 0;
        }
      }

      rep(i, 5) {
        vector<int> tmp(n);
        int now = n-1;
        for(int j = n-1; j>=0;j--) if(t[j][i]) tmp[now--] = t[j][i];
        rep(j, n) t[j][i] = tmp[j];
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 300
memory_kb: 3336
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}

vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (k < 0)return 0;
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}


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

    while (1) {
        ll H;
        cin >> H;
        if (H == 0)return 0;
        vvll A(H, vll(5));
        ll an = 0;
        rep(i, H)rep(j, 5) {
            cin >> A[i][j];
            an += A[i][j];
        }

        rep(d, 50) {
            rep(h, H) {
                vll D = A[h];
                rep(j, 3) {
                    if (D[j] == D[j + 1]&&D[j+1] == D[j + 2]) {
                        A[h][j] = A[h][j + 1] = A[h][j + 2] = 0;
                    }
                }
            }

            rep(h, H) {
                for (ll h = H - 1; h > 0; h--) {
                    rep(j, 5) {
                        if (A[h][j] == 0) {
                            swap(A[h][j], A[h - 1][j]);
                        }
                    }
                }
            }
        }
        rep(h, H)rep(j, 5)an -= A[h][j];
        cout << an << endl;
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3340
final_rank: 5

code_snippet: #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define SP << " " <<

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<P> pvec;
typedef vector<vector<int>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<int> PQI;
typedef priority_queue<P> PQP;
typedef priority_queue<int,vector<int>,greater<int>> PQIG;
typedef priority_queue<P,vector<P>,greater<P>> PQPG;

const vector<int> DX = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> DY = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30); // 1073741824
const ll INF = (1LL << 60); // 1152921504606846976
const double PI = (3.141592653589794);
const double EPS = (0.0000000001); // 10^(-10)

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> inline T ceil(T a, T b) {return T((a + b - 1) / b);}
template<class T> inline T round(T a, T b) {return T(a / b);}
template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; }
template <class T> inline void out(T &a) { bool flag=true; for(auto&x:a){if(flag) {flag=false;} else{ cout << ' '; } cout<<x; } cout << endl; }



//----------------------------------------------------------------
int nmax=200000; // 2*(10^5)
vvec G(nmax);





void solve4ts()
{
  
  
  int h;
  cin>>h;
  if(h==0) exit(0);
  vector<vector<P>> a(5,vector<P>(h));
  rep(i,h){
    rep(j,5){
      int x;cin>>x;
      a[j][h-1-i]=P(h-i,x);
    }
  }
  int ans=0;
  while(true){
    int now=0; // 今回の消す値
    rep(j,5) sort(all(a[j])); // 毎回ソート
    rep(takasa,h){ // 高さごとに見る
      int atai=-1; // 今回の高さで揃った値
      set<int> st;
      rep(j,3){ // 0,1,2 1,2,3, 2,3,4
        if(a[j][takasa].sc==a[j+1][takasa].sc && a[j+1][takasa].sc==a[j+2][takasa].sc && a[j][takasa].sc!=INF){
          atai=a[j][takasa].sc; // 今回の高さの消す値
          st.insert(j);
        }
      }
      for(int index:st){
        rep(j,3){
          if(a[index+j][takasa].sc!=INF) now+=a[index+j][takasa].sc;
          a[index+j][takasa]=P(INF,INF);
        }
      }
    }
    ans+=now; // nowを加算
    if(now==0) break;
  }
  cout<<ans<<endl;
}
//-----------------------------------------------------------------

signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15);
  int repeat = INF;
  // cin >> repeat;
  while(repeat--) solve4ts();
}

/*

g++ -std=c++1z code.cpp

./a.out

python3 expander.py code.cpp

*/
accuracy: 1
time_ms: 10
memory_kb: 14044
final_rank: 13

code_snippet: // https://ei1333.github.io/luzhiled/
// http://beet-aizu.hatenablog.com/entry/2017/01/04/113233
// http://www.prefield.com/algorithm/
#include <bits/stdc++.h>
constexpr char bn = '\n';
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
using Graph = vector<vector<ll>>;
const ll INF = 1LL << 60;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
const ll tl= 1000000007;
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REREP(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, a, n) for (ll i = a; i < n; i++)
#define REFOR(i, n, a) for (ll i = n; i >= a; i--)
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
template<typename T>void die(T x){out(x); exit(0);}
#define in1(A) REP(i,A.size()) in(A[i]);
#define in2(A,B) REP(i,A.size()) in(A[i],B[i]);
#define in3(s,t,u) REP(i,sz(s)){in(s[i] , t[i] , u[i]);}
#define in4(s,t,u,v) REP(i,sz(s)){in(s[i] , t[i] , u[i] , v[i]);}
#define each(x,v) for(auto& x : v)
#define all(v) (v).begin(),(v).end()
#define sz(v) ((int)(v).size())
struct Point{ ld x,y; };
ld dist(Point a, Point b){return sqrt(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y));} // ２点間の距離 
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} 
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
   while (n > 0) {
      if (n & 1) res = res * a % mod;
      a = a * a % mod;
      n >>= 1;
  }
  return res;
}
ll fact(ll n){ if(n < 2) return 1; return (n * fact(n-1))%tl; } //階乗
inline ll updiv(ll a,ll b){ return (a + b - 1) / b; } //切り上げ
template<typename T,typename U>ll ceil(T a,U b){return (a + b - 1) / b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO"  )
#define Yes(n) ((n) ? "Yes" : "No"  )
#define yes(n) ((n) ? "yes" : "no"  )
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
//-------------------------------------------------------------------------------------------------

int main(){
    init();
    
    int h;
    vl an;
    while(cin >> h && h){
        
        vvl A(h,vl(5,0));
        REP(i,h){
            REP(j,5){
                cin >> A[i][j];
            }
        }
        ll ans= 0;
        REP(t,100){
            vvl B(h,vl(5,0)),C(h,vl(5,0));
            REP(i,h){
                FOR(j,1,4){
                    if(A[i][j]!=0 && A[i][j-1]==A[i][j] && A[i][j]==A[i][j+1]){
                        B[i][j-1]=1;
                        B[i][j]=1;
                        B[i][j+1]=1;
                    }
                }
                REP(j,5) if(B[i][j]) ans+= A[i][j];
            }
            
            REP(j,5){
                for(int i=h-2; i>=0; i--){
                    C[i][j]=B[i+1][j]+C[i+1][j];
                }
            }
            REP(j,5){
                for(int i=h-1; i>=0; i--){
                    if(B[i][j]==0){
                        A[i+C[i][j]][j]= A[i][j];
                        if(C[i][j]!=0) A[i][j]=0; 
                    }else{
                        A[i][j]=0;
                    }
                }
            }
            // out("A");
            // REP(i,h){
            //     REP(j,5){
            //         cout << A[i][j] << " ";
            //     }out();
            // }
            // out("B");
            // REP(i,h){
            //     REP(j,5){
            //         cout << B[i][j] << " ";
            //     }out();
            // }
        }
        
        an.push_back(ans);
    }
    REP(i,an.size()) out(an[i]);
    // vl A(n); in1(A)
}
accuracy: 1
time_ms: 20
memory_kb: 3416
final_rank: 8

code_snippet: #include <bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)

ll mod = 1e9+7;

vll dx={1,0,-1};
vll dy={1,0,-1};
int main() {
   while(1){
       ll H;
       cin>>H;
       if(H==0)return 0;
       vector<vll> Q(H,vll(5));
       rep(h,H)rep(i,5)cin>>Q[h][i];
       ll an=0;
       rep(t,100){
           rep(h,H){
               ll L=2,R=2;
               rep(i,2){
                   if(Q[h][R+1]==Q[h][2])R++;
                   else break;
               }
               rep(i,2){
                   if(Q[h][L-1]==Q[h][2])L--;
                   else break;
               }
               if(R-L+1>=3){
                   for(ll i=L;i<=R;i++){
                       an+=Q[h][i];
                       Q[h][i]=0;
                   }
               }
           }
           rep(k,10){
           rep(h,H-1){
               rep(j,5){
                   if(Q[H-h-1][j]==0)swap(Q[H-h-2][j],Q[H-h-1][j]);
                    
               }
               
           }}
           /*
           rep(h,H){
               rep(j,5)cout<<Q[h][j]<<" ";
               cout<<endl;
           }
           cout<<endl;*/
       }
        cout<<an<<endl;
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3336
final_rank: 6