Problem:
J: Tiles are Colorful
ICPC で良い成績を収めるには修行が欠かせない．うさぎは ICPC で勝ちたいので，今日も修行をすることにした．
今日の修行は，流行りのパズルをすばやく解いて，瞬発力を鍛えようというものである．今日挑戦するのは，色とりどりのタイルが並んでいてそれらを上手く消していくパズルだ
初期状態では，グリッド上のいくつかのマスにタイルが置かれている．各タイルには色がついている．プレイヤーはゲーム開始後，以下の手順で示される操作を何回も行うことができる．
タイルが置かれていないマスを 1 つ選択し，そのマスを叩く．
叩いたマスから上に順に辿っていき，タイルが置かれているマスに至ったところでそのタイルに着目する．タイルが置かれているマスがないまま盤面の端に辿り着いたら何にも着目しない．
同様の操作を叩いたマスから下・左・右方向に対して行う．最大 4 枚のタイルが着目されることになる．
着目したタイルの中で同じ色のものがあれば，それらのタイルを盤面から取り除く．同じ色のタイルの組が 2 組あれば，それら両方を取り除く．
タイルを取り除いた枚数と同じ値の得点が入る．
着目をやめる．
たとえば，以下のような状況を考えよう．タイルが置かれていないマスはピリオドで，タイルの色はアルファベット大文字で表されている．
..A.......
.......B..
..........
..B.......
..A.CC....
ここで上から 2 行目，左から 3 列目のマスを叩く操作を考える．着目することになるタイルは
A
,
B
,
B
の 3 枚であるから，
B
の 2 枚が消えて盤面は以下のようになり，2 点を得る．
..A.......
..........
..........
..........
..A.CC....
このパズルはゆっくりしていると時間切れになってしまい，盤面の一部が見えなくなりどのくらい修行が足りなかったのかがわからなくなってしまう．
各色のタイルは 2 枚ずつ置かれているが，それらをすべて消せるとは限らないので，予めプログラムに得点の最大値を計算させておきたい．
Input
M
N
C
1,1
C
1,2
...
C
1,
N
C
2,1
C
2,2
...
C
2,
N
...
C
M
,1
C
M
,2
...
C
M
,
N
整数
M
,
N
は盤が 縦
M
× 横
N
のマス目であることを表す．
C
i
,
j
はアルファベット大文字またはピリオド (
.
) であり，上から
i
行目，左から
j
列目のマスについて，アルファベット大文字の場合は置かれているタイルの色を表し，ピリオドの場合はこのマスにタイルが置かれていないことを表す．
1 ≤
M
≤ 500，1 ≤
N
≤ 500 を満たす．各アルファベット大文字は入力中に 0 個または 2 個現れる．
Output
得点の最大値を 1 行に出力せよ．
Sample Input 1
5 10
..A.......
.......B..
..........
..B.......
..A.CC....
Sample Output 1
4
Sample Input 2
3 3
ABC
D.D
CBA
Sample Output 2
4
Sample Input 3
5 7
NUTUBOR
QT.SZRQ
SANAGIP
LMDGZBM
KLKIODP
Sample Output 3
34

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
using ll = long long;
const long long INF = (1LL<<60);
const long long mod = 998244353;
#define rep(i, n) for (int i = 0; i < n; i++)
int m, n;
vector<string> a;
ll score = 0;
bool update(int i, int j) {
    bool flag = false;
    map<char, vector<pair<int, int>>> ma;
    int scale = 1;
    // sita
    while (i + scale < m) {
        if (a[i+scale][j] != '.') {
            ma[a[i+scale][j]].push_back(make_pair(i+scale, j));
            break;
        }
        scale++;
    }
    scale = 1;
    // down
    while (i - scale >= 0) {
        if (a[i-scale][j] != '.') {
            ma[a[i-scale][j]].push_back(make_pair(i-scale, j));
            break;
        }
        scale++;
    }
    scale = 1;
    while (j + scale < n) {
        if (a[i][j+scale] != '.') {
            ma[a[i][j+scale]].push_back(make_pair(i, j+scale));
            break;
        }
        scale++;
    }
    scale = 1;
    while (j - scale >= 0) {
        if (a[i][j-scale] != '.') {
            ma[a[i][j-scale]].push_back(make_pair(i, j-scale));
            break;
        }
        scale++;
    }
    for (pair<char, vector<pair<int, int>>> p : ma) {
        if (p.second.size() >= 2) {
            for (pair<int, int> px : p.second) {
                a[px.first][px.second] = '.';
                score++;
                flag = true;
            }
        }
    }
    return flag;
}
int main(){
    cin>>m>>n;
    a = vector<string>(m);
    rep(i, m) cin>>a[i];
    while (true) {
        bool flag = false;
        rep(i, m) {
            rep(j, n) {
                if (a[i][j] != '.') continue;
                if (update(i, j)) {
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) break;
    }
    cout<<score<<endl;
}
accuracy: 1
time_ms: 360
memory_kb: 3936
final_rank: 6

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

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(), x.end()
template <class A, class B>
inline bool chmin(A &a, const B &b) {
    return b < a && (a = b, true);
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
    return b > a && (a = b, true);
}

int main() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, h) cin >> s[i];

    vector<bool> used(26);
    vector<pair<int, int>> st;

    auto get = [&](int i, int j, int di, int dj) -> tuple<int, int, int> {
        assert(s[i][j] == '.');
        while (0 <= i && i < h && 0 <= j && j < w && s[i][j] == '.') {
            i += di;
            j += dj;
        }
        if (0 <= i && i < h && 0 <= j && j < w) return {i, j, s[i][j] - 'A'};
        return {-1, -1, -1};
    };

    const int DI[] = {1, 0, -1, 0};
    const int DJ[] = {0, 1, 0, -1};
    rep(i, h) rep(j, w) {
        if (s[i][j] != '.') continue;
        vector<int> count(26);
        rep(d, 4) {
            auto [ni, nj, v] = get(i, j, DI[d], DJ[d]);
            if (v != -1) count[v]++;
        }
        rep(k, 26) if (count[k] == 2 && !used[k]) {
            used[k] = true;
            st.emplace_back(i, j);
            break;
        }
    }
    int ans = 0;
    while (!st.empty()) {
        auto [ci, cj] = st.back();
        st.pop_back();
        vector<vector<pair<int, int>>> count(26);
        rep(d, 4) {
            auto [ni, nj, v] = get(ci, cj, DI[d], DJ[d]);
            if (v != -1) {
                count[v].emplace_back(ni, nj);
            }
        }
        rep(k, 26) if (count[k].size() == 2) {
            ans++;
            for (auto [i, j]: count[k]) {
                assert(s[i][j] == k + 'A');
                s[i][j] = '.';
                rep(a, h) if (s[a][j] == '.') st.emplace_back(a, j);
                rep(b, w) if (s[i][b] == '.') st.emplace_back(i, b);
            }
        }
    }
    cout << 2 * ans << '\n';
}
accuracy: 1
time_ms: 270
memory_kb: 3972
final_rank: 4

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

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define all(x) x.begin(), x.end()
template <class A, class B>
inline bool chmin(A &a, const B &b) {
    return b < a && (a = b, true);
}
template <class A, class B>
inline bool chmax(A &a, const B &b) {
    return b > a && (a = b, true);
}

int main() {
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    rep(i, h) cin >> s[i];

    vector<bool> used(26);
    vector<pair<int, int>> st;

    auto get = [&](int i, int j, int di, int dj) -> tuple<int, int, int> {
        assert(s[i][j] == '.');
        while (0 <= i && i < h && 0 <= j && j < w && s[i][j] == '.') {
            i += di;
            j += dj;
        }
        if (0 <= i && i < h && 0 <= j && j < w) return {i, j, s[i][j] - 'A'};
        return {-1, -1, -1};
    };

    const int DI[] = {1, 0, -1, 0};
    const int DJ[] = {0, 1, 0, -1};
    rep(i, h) rep(j, w) {
        if (s[i][j] != '.') continue;
        vector<int> count(26);
        rep(d, 4) {
            auto [ni, nj, v] = get(i, j, DI[d], DJ[d]);
            if (v != -1) count[v]++;
        }
        rep(k, 26) if (count[k] == 2 && !used[k]) {
            used[k] = true;
            st.emplace_back(i, j);
            break;
        }
    }
    int ans = 0;
    while (!st.empty()) {
        auto [ci, cj] = st.back();
        st.pop_back();
        vector<vector<pair<int, int>>> count(26);
        rep(d, 4) {
            auto [ni, nj, v] = get(ci, cj, DI[d], DJ[d]);
            if (v != -1) {
                count[v].emplace_back(ni, nj);
            }
        }
        bool ok = false;
        rep(k, 26) if (count[k].size() == 2) {
            ans++;
            ok = true;
            for (auto [i, j]: count[k]) {
                assert(s[i][j] == k + 'A');
                s[i][j] = '.';
                st.emplace_back(i, j);
            }
        }
        if (ok) st.emplace_back(ci, cj);
    }
    cout << 2 * ans << '\n';
}
accuracy: 0.14754098360655737
time_ms: 200
memory_kb: 3860
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using ll = long long;
#define var auto
const char newl = '\n';

using namespace std;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> grid(h);
  for (var&& s : grid) cin >> s;

  int res = 0;
  while (true) {


    bool changed = false;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (grid[i][j] != '.') continue;
        vector<char*> cs;
        for (int i2 = i; i2 < h; i2++) {
          if (grid[i2][j] != '.') {
            cs.emplace_back(&grid[i2][j]);
            break;
          }
        }
        for (int i2 = i; i2 >= 0; i2--) {
          if (grid[i2][j] != '.') {
            cs.emplace_back(&grid[i2][j]);
            break;
          }
        }

        for (int j2 = j; j2 < w; j2++) {
          if (grid[i][j2] != '.') {
            cs.emplace_back(&grid[i][j2]);
            break;
          }
        }
        for (int j2 = j; j2 >= 0; j2--) {
          if (grid[i][j2] != '.') {
            cs.emplace_back(&grid[i][j2]);
            break;
          }
        }
        for (int i = 0; i < cs.size(); i++) {
          for (int j = i + 1; j < cs.size(); j++) {
            if (*cs[i] == *cs[j] && *cs[i] != '.') {
              *cs[i] = '.';
              *cs[j] = '.';
              res += 2;
              changed = true;
            }
          }
        }
      }
    }
    if (not changed) break;
  }
  cout << res << endl;
}
accuracy: 1
time_ms: 360
memory_kb: 3928
final_rank: 5

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
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;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> G(H);
    REP(i, H) cin >> G[i];
    auto is_in = [&](int i, int j) -> bool {
        return (0 <= i and i < H and 0 <= j and j < W);
    };

    int ans = 0;
    while(1) {
        bool upd = false;
        map<char, vector<pair<int, int>>> mp;
        REP(i, H) REP(j, W) if(G[i][j] == '.') {
            mp.clear();
            REP(dir, 4) {
                int ni = i, nj = j;
                while(is_in(ni, nj)) {
                    if(G[ni][nj] != '.') {
                        mp[G[ni][nj]].emplace_back(ni, nj);
                        break;
                    }
                    ni += dx[dir], nj += dy[dir];
                }
            }
            for(const auto& [key, v] : mp) {
                if(v.size() == 2) {
                    for(auto [ni, nj] : v) G[ni][nj] = '.';
                    ans += 2;
                    upd = true;
                } else assert(v.size() <= 1);
            }
        }
        if(!upd) break;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 840
memory_kb: 3720
final_rank: 8

code_snippet: #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
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;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> G(H);
    REP(i, H) cin >> G[i];
    auto is_in = [&](int i, int j) -> bool {
        return (0 <= i and i < H and 0 <= j and j < W);
    };

    int ans = 0;
    while(1) {
        bool upd = false;
        auto loop1 = [&]() {
            map<char, vector<pair<int, int>>> mp;
            REP(i, H) REP(j, W) if(G[i][j] == '.') {
                mp.clear();
                REP(dir, 4) {
                    int ni = i, nj = j;
                    while(is_in(ni, nj)) {
                        if(G[ni][nj] != '.') {
                            mp[G[ni][nj]].emplace_back(ni, nj);
                            break;
                        }
                        ni += dx[dir], nj += dy[dir];
                    }
                }
                for(const auto& [key, v] : mp) {
                    if(v.size() == 2) {
                       for(auto [ni, nj] : v) G[ni][nj] = '.';
                       ans += 2;
                       upd = true;
                    } else assert(v.size() <= 1);
                }
            }
        }; loop1();
        if(!upd) break;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 840
memory_kb: 3720
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int M,N; cin >> M >> N;
    vector<string> C(M);
    for (int i=0;i<M;++i) cin >> C[i];

    vector<vector<pair<int,int>>> pos(26);
    for (int i=0;i<M;++i){
        for (int j=0;j<N;++j){
            if (C[i][j]=='.') continue;
            pos[C[i][j]-'A'].emplace_back(i,j);
        }
    }

    vector<pair<int,int>> cand;
    for (int i=0;i<26;++i){
        if (pos[i].empty()) continue;
        auto p=pos[i][0],q=pos[i][1];
        if (p.first==q.first){
            if (p.second>q.second) swap(p,q);
            for (int j=p.second+1;j<q.second;++j){
                cand.emplace_back(p.first,j);
            }
        } else if (p.second==q.second){
            if (p.first>q.first) swap(p,q);
            for (int j=p.first+1;j<q.first;++j){
                cand.emplace_back(j,p.second);
            }
        } else {
            cand.emplace_back(p.first,q.second);
            cand.emplace_back(q.first,p.second);
        }
    }

    int ans=0;
    auto search=[&](int x,int y,int i){
        while (1){
            x+=dx[i]; y+=dy[i];
            if (x<0||M<=x||y<0||N<=y) break;
            if (C[x][y]=='.') continue;
            return C[x][y];
        }
        return '.';
    };

    auto erase=[&](int i){
        C[pos[i][0].first][pos[i][0].second]='.';
        C[pos[i][1].first][pos[i][1].second]='.';
        ans+=2;
    };

    auto check=[&](pair<int,int> p){
        int x=p.first,y=p.second;
        if (C[x][y]!='.') return;
        vector<char> v;
        for (int i=0;i<4;++i){
            char c=search(x,y,i);
            if (c!='.') v.emplace_back(c);
        }
        for (int i=0;i<v.size();++i){
            for (int j=i+1;j<v.size();++j){
                if (v[i]==v[j]){
                    erase(v[i]-'A');
                }
            }
        }
    };

    for (int _=0;_<26;++_){
        for (auto p:cand){
            check(p);
        }
    }

    cout << ans << '\n';
}
accuracy: 1
time_ms: 170
memory_kb: 3360
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;
// const long long MOD=998244353;
#define LOCAL
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(),(x).end()
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U,typename V>
ostream&operator<<(ostream &os,const tuple<T,U,V> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
    return os;
}
template<typename T,typename U,typename V,typename W>
ostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){
    os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const unordered_map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const multiset<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const unordered_set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();){
        os << *itr;
        if (++itr!=s.end()) os << ',';
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const deque<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

template<class T1,class T2> inline bool chmin(T1 &a,T2 b){
    if (a>b){a=b; return true;} return false;
}
template<class T1,class T2> inline bool chmax(T1 &a,T2 b){
    if (a<b){a=b; return true;} return false;
}
#pragma endregion

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int M,N; cin >> M >> N;
    vector<string> C(M);
    for (int i=0;i<M;++i) cin >> C[i];

    vector<vector<pair<int,int>>> pos(26);
    for (int i=0;i<M;++i){
        for (int j=0;j<N;++j){
            if (C[i][j]=='.') continue;
            pos[C[i][j]-'A'].emplace_back(i,j);
        }
    }

    vector<pair<int,int>> cand;
    for (int i=0;i<26;++i){
        if (pos[i].empty()) continue;
        auto p=pos[i][0],q=pos[i][1];
        if (p.first==q.first){
            if (p.second>q.second) swap(p,q);
            for (int j=p.second+1;j<q.second;++j){
                cand.emplace_back(p.first,j);
            }
        } else if (p.second==q.second){
            if (p.first>q.first) swap(p,q);
            for (int j=p.first+1;j<q.first;++j){
                cand.emplace_back(j,p.second);
            }
        } else {
            cand.emplace_back(p.first,q.second);
            cand.emplace_back(q.first,p.second);
        }
    }

    int ans=0;
    auto search=[&](int x,int y,int i){
        while (1){
            x+=dx[i]; y+=dy[i];
            if (x<0||M<=x||y<0||N<=y) break;
            if (C[x][y]=='.') continue;
            return C[x][y];
        }
        return '.';
    };

    auto erase=[&](int i){
        C[pos[i][0].first][pos[i][0].second]='.';
        C[pos[i][1].first][pos[i][1].second]='.';
        ans+=2;
    };

    auto check=[&](pair<int,int> p){
        int x=p.first,y=p.second;
        if (C[x][y]!='.') return;
        vector<char> v;
        for (int i=0;i<4;++i){
            char c=search(x,y,i);
            if (c!='.') v.emplace_back(c);
        }
        for (int i=0;i<v.size();++i){
            for (int j=i+1;j<v.size();++j){
                if (v[i]==v[j]){
                    erase(v[i]-'A');
                }
            }
        }
    };

    for (int _=0;_<26;++_){
        for (auto p:cand){
            check(p);
        }
    }

    cout << ans << '\n';
}
accuracy: 1
time_ms: 170
memory_kb: 3360
final_rank: 2

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

using namespace std;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  REP(i, SZ(v)) {
    if (i) os << ", ";
    os << v[i];
  }
  return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n, m; cin >> n >> m;
  vector<string> s(n);
  map<char, pair<P, P>> mp;
  REP(i, n) {
    cin >> s[i];
    REP(j, m) {
      if(s[i][j] != '.') {
        if(mp.find(s[i][j]) == mp.end()) {
          mp[s[i][j]] = {{i, j}, {-1, -1}};
        } else {
          mp[s[i][j]].second = {i, j};
        }
      }
    }
  }

  map<char, vector<P>> koho;
  for(auto &e: mp) {
    int i1, j1, i2, j2;
    tie(i1, j1) = e.second.first;
    tie(i2, j2) = e.second.second;

    vector<P> v;
    if(i1 == i2) {
      for(int j = min(j1, j2)+1; j < max(j1, j2);++j) {
        v.emplace_back(i1, j);
      }
    } else if(j1 == j2) {
      for(int i = min(i1, i2)+1; i < max(i1, i2);++i) {
        v.emplace_back(i, j1);
      }
    } else {
      v.emplace_back(i1, j2);
      v.emplace_back(i2, j1);
    }
    koho[e.first] = v;
  }

  int ans = 0;
  bool upd = true;
  while(upd) {
    upd = false;

    for(auto &e1: koho) {
      for(auto &e2: e1.second) {
        int i1, j1;
        tie(i1, j1) = e2;
        int i2, j2;
        tie(i2, j2) = mp[e1.first].first;

        bool ok = true;
        if(j1 == j2) {
          for (int i = min(i1, i2); i <= max(i1, i2); ++i) {
            ok &= s[i][j1] == '.' || s[i][j1] == e1.first;
          }
        } else {
          for(int j = min(j1, j2); j <= max(j1, j2); ++j) {
            ok &= s[i1][j] == '.' || s[i1][j] == e1.first;
          }
        }

        tie(i2, j2) = mp[e1.first].second;
        if(j1 == j2) {
          for (int i = min(i1, i2); i <= max(i1, i2); ++i) {
            ok &= s[i][j1] == '.' || s[i][j1] == e1.first;
          }
        } else {
          for(int j = min(j1, j2); j <= max(j1, j2); ++j) {
            ok &= s[i1][j] == '.' || s[i1][j] == e1.first;
          }
        }
        if(ok) {
          ans += 2;
          s[i2][j2] = '.';
          tie(i2, j2) = mp[e1.first].first;
          s[i2][j2] = '.';
          upd = true;
          break;
        }
      }

      if(upd) {
        koho.erase(e1.first);
        break;
      }
    }

    if(!upd) break;
  }

  cout << ans << endl;








  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3672
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    clock_t start = clock();
    auto ord = [](char c) {
        if (c < 'a') {
            return int(c - 'A');
        }
        return int(26 + c - 'a');
    };

    int m, n;
    cin >> m >> n;
    vector<string> v(m);
    vector<bool> check(26);
    vector<int> ys(52), xs(52);
    for (int i = 0; i < m; i++) {
        cin >> v[i];
        for (int j = 0; j < n; j++) {
            char c = v[i][j];
            if (c == '.') continue;
            if (!check[ord(c)]) {
                check[ord(c)] = 1;
                c -= ('A' - 'a');
            }
            ys[ord(c)] = i, xs[ord(c)] = j;
        }
    }

    vector<set<int>> sty(n), stx(m);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (v[i][j] != '.') stx[i].insert(j);
        }
    }
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            if (v[i][j] != '.') sty[j].insert(i);
        }
    }

    auto ERASE = [&](char a, char b) {
        int ya = ys[ord(a)], xa = xs[ord(a)];
        int yb = ys[ord(b)], xb = xs[ord(b)];
        if (ya == yb) {
            if (xa > xb) swap(xa, xb);
            if (xb - xa == 1) return 0;
            if (*stx[ya].upper_bound(xa) == xb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }
        if (xa == xb) {
            if (ya > yb) swap(ya, yb);
            if (yb - ya == 1) return 0;
            if (*sty[xa].upper_bound(ya) == yb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }

        if (xa > xb) {
            swap(xa, xb);
            swap(ya, yb);
        }
        if (ya > yb) {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == sty[xa].begin() || *(--itra) < yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        } else {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == (--sty[xa].end()) || *(++itra) > yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == sty[xb].begin() || *(--itrb) < ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        }
    };

    queue<pair<char, char>> q;
    for (int i = 0; i < 26; i++)
        if (check[i]) q.emplace('A' + i, 'a' + i);

    int ans = 0;
    while (!q.empty()) {
        auto [a, b] = q.front();
        q.pop();
        if (ERASE(a, b))
            ans += 2;
        else
            q.emplace(a, b);
        clock_t end = clock();
        const double time =
            static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
        if (time > 1900) break;
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 1900
memory_kb: 3992
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    clock_t start = clock();
    auto ord = [](char c) {
        if (c < 'a') {
            return int(c - 'A');
        }
        return int(26 + c - 'a');
    };

    int m, n;
    cin >> m >> n;
    vector<string> v(m);
    vector<bool> check(26);
    vector<int> ys(52), xs(52);
    for (int i = 0; i < m; i++) {
        cin >> v[i];
        for (int j = 0; j < n; j++) {
            char c = v[i][j];
            if (c == '.') continue;
            if (!check[ord(c)]) {
                check[ord(c)] = 1;
                c -= ('A' - 'a');
            }
            ys[ord(c)] = i, xs[ord(c)] = j;
        }
    }

    vector<set<int>> sty(n), stx(m);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (v[i][j] != '.') stx[i].insert(j);
        }
    }
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            if (v[i][j] != '.') sty[j].insert(i);
        }
    }

    auto erase = [&](char a, char b) {
        int ya = ys[ord(a)], xa = xs[ord(a)];
        int yb = ys[ord(b)], xb = xs[ord(b)];
        if (ya == yb) {
            if (xa > xb) swap(xa, xb);
            if (xb - xa == 1) return 0;
            if (*stx[ya].upper_bound(xa) == xb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }
        if (xa == xb) {
            if (ya > yb) swap(ya, yb);
            if (yb - ya == 1) return 0;
            if (*sty[xa].upper_bound(ya) == yb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }

        if (xa > xb) {
            swap(xa, xb);
            swap(ya, yb);
        }
        if (ya < yb) {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == sty[xa].begin() || *(--itra) < yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        } else {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == (--sty[xa].end()) || *(++itra) > yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == sty[xb].begin() || *(--itrb) < ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        }
    };

    queue<pair<char, char>> q;
    for (int i = 0; i < 26; i++)
        if (check[i]) q.emplace('A' + i, 'a' + i);

    int ans = 0;
    int cnt = 0;
    while (!q.empty()) {
        auto [a, b] = q.front();
        q.pop();
        if (erase(a, b))
            ans += 2;
        else
            q.emplace(a, b);
        clock_t end = clock();
        const double time =
            static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
        if (time > 1900) break;
    }
    cout << ans << '\n';
}
accuracy: 0.14754098360655737
time_ms: 1900
memory_kb: 3752
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    clock_t start = clock();
    auto ord = [](char c) {
        if (c < 'a') {
            return int(c - 'A');
        }
        return int(26 + c - 'a');
    };

    int m, n;
    cin >> m >> n;
    vector<string> v(m);
    vector<bool> check(26);
    vector<int> ys(52), xs(52);
    for (int i = 0; i < m; i++) {
        cin >> v[i];
        for (int j = 0; j < n; j++) {
            char c = v[i][j];
            if (c == '.') continue;
            if (!check[ord(c)]) {
                check[ord(c)] = 1;
                c -= ('A' - 'a');
            }
            ys[ord(c)] = i, xs[ord(c)] = j;
        }
    }

    vector<set<int>> sty(52), stx(52);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (v[i][j] != '.') stx[i].insert(j);
        }
    }
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            if (v[i][j] != '.') sty[j].insert(i);
        }
    }

    auto erase = [&](char a, char b) {
        int ya = ys[ord(a)], xa = xs[ord(a)];
        int yb = ys[ord(b)], xb = xs[ord(b)];
        if (ya == yb) {
            if (xa > xb) swap(xa, xb);
            if (xb - xa == 1) return 0;
            if (*stx[ya].upper_bound(xa) == xb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }
        if (xa == xb) {
            if (ya > yb) swap(ya, yb);
            if (yb - ya == 1) return 0;
            if (*sty[xa].upper_bound(ya) == yb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }

        if (xa > xb) {
            swap(xa, xb);
            swap(ya, yb);
        }
        if (ya < yb) {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == sty[xa].begin() || *(--itra) < yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        } else {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == (--sty[xa].end()) || *(++itra) > yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == sty[xb].begin() || *(--itrb) < ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        }
    };

    queue<pair<char, char>> q;
    for (int i = 0; i < 26; i++)
        if (check[i]) q.emplace('A' + i, 'a' + i);

    int ans = 0;
    int cnt = 0;
    while (!q.empty()) {
        auto [a, b] = q.front();
        q.pop();
        if (erase(a, b))
            ans += 2;
        else
            q.emplace(a, b);
        clock_t end = clock();
        const double time =
            static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
        if (time > 1000) break;
    }
    cout << ans << '\n';
}
accuracy: 0.08196721311475409
time_ms: 990
memory_kb: 3864
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main() {
    clock_t start = clock();
    auto ord = [](char c) {
        if (c < 'a') {
            return int(c - 'A');
        }
        return int(26 + c - 'a');
    };

    int m, n;
    cin >> m >> n;
    vector<string> v(m);
    vector<bool> check(26);
    vector<int> ys(52), xs(52);
    for (int i = 0; i < m; i++) {
        cin >> v[i];
        for (int j = 0; j < n; j++) {
            char c = v[i][j];
            if (c == '.') continue;
            if (!check[ord(c)]) {
                check[ord(c)] = 1;
                c -= ('A' - 'a');
            }
            ys[ord(c)] = i, xs[ord(c)] = j;
        }
    }

    vector<set<int>> sty(52), stx(52);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (v[i][j] != '.') stx[i].insert(j);
        }
    }
    for (int j = 0; j < n; j++) {
        for (int i = 0; i < m; i++) {
            if (v[i][j] != '.') sty[j].insert(i);
        }
    }

    auto erase = [&](char a, char b) {
        int ya = ys[ord(a)], xa = xs[ord(a)];
        int yb = ys[ord(b)], xb = xs[ord(b)];
        if (ya == yb) {
            if (xa > xb) swap(xa, xb);
            if (xb - xa == 1) return 0;
            if (*stx[ya].upper_bound(xa) == xb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }
        if (xa == xb) {
            if (ya > yb) swap(ya, yb);
            if (yb - ya == 1) return 0;
            if (*sty[xa].upper_bound(ya) == yb) {
                stx[ya].erase(xa);
                stx[yb].erase(xb);
                sty[xa].erase(ya);
                sty[xb].erase(yb);
                return 1;
            }
            return 0;
        }

        if (xa > xb) {
            swap(xa, xb);
            swap(ya, yb);
        }
        if (ya < yb) {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == sty[xa].begin() || *(--itra) < yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == (--sty[xb].end()) || *(++itrb) > ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        } else {
            {
                bool ok = 1;
                auto itra = sty[xa].find(ya);
                ok &= itra == (--sty[xa].end()) || *(++itra) > yb;
                auto itrb = stx[yb].find(xb);
                ok &= itrb == stx[yb].begin() || *(--itrb) < xa;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            {
                bool ok = 1;
                auto itra = stx[ya].find(xa);
                ok &= itra == (--stx[ya].end()) || *(++itra) > xb;
                auto itrb = sty[xb].find(yb);
                ok &= itrb == sty[xb].begin() || *(--itrb) < ya;
                if (ok) {
                    stx[ya].erase(xa);
                    stx[yb].erase(xb);
                    sty[xa].erase(ya);
                    sty[xb].erase(yb);
                    return 1;
                }
            }
            return 0;
        }
    };

    queue<pair<char, char>> q;
    for (int i = 0; i < 26; i++)
        if (check[i]) q.emplace('A' + i, 'a' + i);

    int ans = 0;
    int cnt = 0;
    while (!q.empty()) {
        auto [a, b] = q.front();
        q.pop();
        if (erase(a, b))
            ans += 2;
        else
            q.emplace(a, b);
        clock_t end = clock();
        const double time =
            static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
        if (time > 1900) break;
    }
    cout << ans << '\n';
}
accuracy: 0.08196721311475409
time_ms: 1890
memory_kb: 3848
final_rank: 20

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

using Pii = pair<int, int>;

int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

int H, W;
vector<string> grid;
vector<int> erase_c[501][501];
vector<Pii> c[26];
int ans = 0;
bool used[501][501];

int main() {
  cin >> H >> W;
  grid.resize(H);

  for ( int i = 0; i < H; i++ ) {
    cin >> grid[i];
    for ( int j = 0; j < W; j++ ) {
      if ( grid[i][j] == '.' ) continue;
      c[grid[i][j]-'A'].push_back(Pii(i, j));      
    }
  }  

  bool flag = true;
  while ( flag ) {
    flag = false;
    for ( int i = 0; i < H; i++ ) {
      for ( int j = 0; j < W; j++ ) {
	if ( grid[i][j] != '.' ) continue;
	vector<int> cnt(26, 0);       
	for ( int k = 0; k < 4; k++ ) {
	  int nx = j+dx[k], ny = i+dy[k];
	  while ( 1 ) {
	    if ( nx < 0 || ny < 0 || nx >= W || ny >= H ) break;
	    if ( grid[ny][nx] != '.' ) {
	      cnt[grid[ny][nx]-'A']++;
	      break;
	    }
	    nx += dx[k]; ny += dy[k];
	  }
	}

	for ( int k = 0; k < 26; k++ ) {
	  if ( cnt[k] == 2 ) {
	    ans += 2;
	    for ( Pii p: c[k] ) grid[p.first][p.second] = '.';
	    flag = true;
	    break;	    
	  }
	}
      }
    }
  }

  cout << ans << endl;  
  
  return 0;
}
accuracy: 1
time_ms: 1140
memory_kb: 9252
final_rank: 14

code_snippet: #include <bits/stdc++.h> 
using namespace std;
#define FOR(i,l,r) for(int i=(l); i<(r); i++)
#define REP(i,n) FOR(i,0,n)
#define int long long 
#define endl "\n"
#define debug(x) cout<<x<<endl;
static const int INF = 1e9+7;

int N,M;
char C[550][550];
vector<pair<int,int> > p;

int search(int x, int y){
    map<char,int> mp;

    //左を見る
    for(int j=x-1; j>=0; j--){
        if(C[y][j] != '.'){
            mp[C[y][j]]++;
            break;
        }
    }

    //右を見る
    for(int j=x+1; j<N; j++){
        if(C[y][j] != '.'){
            mp[C[y][j]]++;
            break;
        }
    }

    //下を見る
    for(int i=y+1; i<M; i++){
        if(C[i][x] != '.'){
            mp[C[i][x]]++;
            break;
        }
    }

    for(int i=y-1; i>=0; i--){
        if(C[i][x] != '.'){
            mp[C[i][x]]++;
            break;
        }
    }

    int add = 0;

    for(auto a : mp){
        if(a.second == 2){
            for(int i=0; i<M; i++){
                for(int j=0; j<N; j++){
                    if(C[i][j] == a.first){
                        C[i][j] = '.';
                        p.push_back({i,j});
                    }
                }
            }
            add += 2;
        }
    }

    return add;
}

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

    cin>>M>>N;

    REP(i,M){
        REP(j,N){
            cin>>C[i][j];
            if(C[i][j] == '.') p.push_back({i,j});
        }
    }

    int ans = 0;

    while(1){
        bool flag = false;

        for(auto a : p){
            int y = a.first;
            int x = a.second;
            int add;

            if((add = search(x,y))){
                flag = true;
                ans += add;
            }
        }

        if(!flag) break;

        /*REP(i,M){
            REP(j,N) debug(C[i][j]);
        }*/
    }

    /*for(auto a : p){
        debug(a.first);
        debug(a.second);
    }*/

    cout<<ans<<endl;
}
accuracy: 1
time_ms: 310
memory_kb: 9436
final_rank: 13

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

using Pii = pair<int, int>;

int H, W;
vector<string> grid;
vector<int> erase_c[501][501];
vector<Pii> c[26];
int ans = 0;
bool used[501][501];

void dfs(int h, int w) {
  if ( used[h][w] ) return;
  if ( grid[h][w] != '.' ) ans++;
  used[h][w] = true;
  // cout << h << " " << w << " " << grid[h][w] << endl;  

  for ( int a: erase_c[h][w] ) {
    for ( Pii p: c[a] ) {
      int y = p.first, x = p.second;
      if ( used[y][x] ) continue;
      dfs(y, x);      
    }
  }
}

int main() {
  cin >> H >> W;
  grid.resize(H);

  for ( int i = 0; i < H; i++ ) {
    cin >> grid[i];
    for ( int j = 0; j < W; j++ ) {
      if ( grid[i][j] == '.' ) continue;
      c[grid[i][j]-'A'].push_back(Pii(i, j));      
    }
  }  

  for ( int a = 0; a < 26; a++ ) {    
    if ( c[a].size() == 0 ) continue;

    for ( int i = 0; i < H; i++ ) {
      for ( int j = 0; j < W; j++ ) {
	if ( (i == c[a][0].first && j == c[a][1].second) || 
	     (i == c[a][1].first && j == c[a][0].second) ||
	     (i == c[a][0].first && i == c[a][1].first) ||
	     (j == c[a][0].second && j == c[a][1].second) ) {
	  // cout << " " << a << endl;	  
	  if ( c[a][0].first == c[a][1].first ) {
	    if ( (c[a][0].second <= j && j <= c[a][1].second ) ||
		 (c[a][1].second <= j && j <= c[a][0].second ) ) {
	      // cout << i << " " << j << " " << a << endl;	    
	      erase_c[i][j].push_back(a);
	    }
	  } else if ( c[a][0].first == c[a][1].first ) {	    
	    if ( (c[a][0].first <= i && i <= c[a][1].first ) ||
		 (c[a][1].first <= i && i <= c[a][0].first ) ) erase_c[i][j].push_back(a);
	  }
	  else erase_c[i][j].push_back(a);
	  
	}
      }
    }
  }

  for ( int i = 0; i < H; i++ ) {
    for ( int j = 0; j < W; j++ ) {
      if ( used[i][j] || grid[i][j] != '.' ) continue;
      dfs(i, j);
    }
  }

  cout << ans << endl;  
  
  return 0;
}
accuracy: 0.14754098360655737
time_ms: 10
memory_kb: 9280
final_rank: 16

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

using Pii = pair<int, int>;

int H, W;
vector<string> grid;
vector<int> erase_c[501][501];
vector<Pii> c[26];
int ans = 0;
bool used[501][501];

void dfs(int h, int w) {
  if ( used[h][w] ) return;
  if ( grid[h][w] != '.' ) ans++;
  used[h][w] = true;
  // cout << h << " " << w << " " << grid[h][w] << endl;  

  for ( int a: erase_c[h][w] ) {
    for ( Pii p: c[a] ) {
      int y = p.first, x = p.second;
      if ( used[y][x] ) continue;
      dfs(y, x);      
    }
  }
}

int main() {
  cin >> H >> W;
  grid.resize(H);

  for ( int i = 0; i < H; i++ ) {
    cin >> grid[i];
    for ( int j = 0; j < W; j++ ) {
      if ( grid[i][j] == '.' ) continue;
      c[grid[i][j]-'A'].push_back(Pii(i, j));      
    }
  }  

  for ( int a = 0; a < 26; a++ ) {    
    if ( c[a].size() == 0 ) continue;

    for ( int i = 0; i < H; i++ ) {
      for ( int j = 0; j < W; j++ ) {
	if ( (i == c[a][0].first && j == c[a][1].second) || 
	     (i == c[a][1].first && j == c[a][0].second) ||
	     (i == c[a][0].first && i == c[a][1].first) ||
	     (j == c[a][0].second && j == c[a][1].second) ) {
	  // cout << " " << a << endl;	  
	  if ( c[a][0].first == c[a][1].first ) {
	    if ( (c[a][0].second <= j && j <= c[a][1].second ) ||
		 (c[a][1].second <= j && j <= c[a][0].second ) ) {
	      // cout << i << " " << j << " " << a << endl;	    
	      erase_c[i][j].push_back(a);
	    }
	  } else if ( c[a][0].first == c[a][1].first ) {	    
	    if ( (c[a][0].first <= j && j <= c[a][1].first ) ||
		 (c[a][1].first <= j && j <= c[a][0].first ) ) erase_c[i][j].push_back(a);
	  }
	  else erase_c[i][j].push_back(a);
	  
	}
      }
    }
  }

  for ( int i = 0; i < H; i++ ) {
    for ( int j = 0; j < W; j++ ) {
      if ( used[i][j] || grid[i][j] != '.' ) continue;
      dfs(i, j);
    }
  }

  cout << ans << endl;  
  
  return 0;
}
accuracy: 0.14754098360655737
time_ms: 10
memory_kb: 9420
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define lp(i,n) for(int i=0;i<(int)(n);i++)
#define lps(i,j,n) for(int i=j;i<n;i++)

#define fordebug int hoge;cin>>hoge;

#define DEKAI 1000000007
#define floot10 cout<<fixed<<setprecision(15)
#define all(v) v.begin(),v.end()
double PI = acos(-1);

int h,w;
int score=0;
bool act(vector<string> &v,int x, int y){
  bool res = false;
  int dx[4]={0,0,1,-1};
  int dy[4]={1,-1,0,0};
  map<char,int> m;
  vector<char> check={};
  lp(i,4){
    int nx=x;
    int ny=y;
    while(1){
      nx+=dx[i];
      ny+=dy[i];
      if(nx<0||nx>=h)break;
      if(ny<0||ny>=w)break;
      if(v[nx][ny]!='.'){
        char c=v[nx][ny];
        if(m[c]==0)m[c]=1;
        else{
          check.push_back(c);
        }
        break;
      }
    }
  }
  lp(i,4){
    int nx=x;
    int ny=y;
    while(1){
      nx+=dx[i];
      ny+=dy[i];
      if(nx<0||nx>=h)break;
      if(ny<0||ny>=w)break;
      if(v[nx][ny]!='.'){
        char c=v[nx][ny];
        lp(j,check.size()){
          if(c==check[j]){
            v[nx][ny]='.';
            res=true;
            score++;
          }
        }
        break;
      }
    }
  }
  return res;
}

void priv(vector<string> &v){
  lp(i,h){
    cout<<v[i]<<endl;
  }
}

signed main(){
  cin>>h>>w;
  vector<string> v(h);
  lp(i,h){
    cin>>v[i];
  }
  while(1){
    bool check = false;
    //priv(v);
    //cout<<score<<endl<<endl;
    lp(i,h){
      lp(j,w){
        if(v[i][j]!='.')continue;
        if(act(v,i,j))check=true;
      }
    }
    if(check==false)break;
  }
  cout<<score<<endl;
  return 0;
}
accuracy: 1
time_ms: 1120
memory_kb: 3920
final_rank: 11

code_snippet: #include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 2020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-10;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;

bool in(ll y, ll x, ll h, ll w){
	return 0 <= y && y < h && 0 <= x && x < w;
}

int main(){
	int n,m; cin >> n >> m;
	vs s(n); rep(i,n) cin >> s[i];
	vector<vpl> pos(26,vpl(2,{-1,-1}));
	rep(i,n){
		rep(j,m){
			if(s[i][j] == '.') continue;
			if(pos[s[i][j]-'A'][0].first == -1){
				pos[s[i][j]-'A'][0] = {i,j};
			}else{
				pos[s[i][j]-'A'][1] = {i,j};
			}
		}
	}
	int ans = 0;
	while(1){
		bool f = false;
		rep(i,n){
			rep(j,m){
				if(s[i][j] != '.') continue;
				vl cnt(26,0);
				rep(k,4){
					for(int t=0;; t++){
						int ny = i + dy[k] * t;
						int nx = j + dx[k] * t;
						if(!in(ny,nx,n,m)) break;
						if(s[ny][nx] != '.'){
							cnt[s[ny][nx]-'A']++;
							break;
						}
					}
				}
				rep(k,26){
					if(cnt[k] == 2){
						rep(l,2) s[pos[k][l].first][pos[k][l].second] = '.';
						ans += 2;
						f = true;
					}
				}
			}
		}
		if(!f) break;
	}
	cout << ans << "\n";
}
accuracy: 1
time_ms: 1290
memory_kb: 3340
final_rank: 10

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000
int ans = 0;

void del(vector<string> &S,int y,int x){
	if(S[y][x]!='.')return;
	vector<int> dy = {0,-1,0,1},dx = {1,0,-1,0};
	rep(i,4){
		rep(j,4){
			if(i==j)continue;
			
			int y1 = y,x1 = x,y2 = y,x2 = x;
			while(true){
				y1 += dy[i];
				x1 += dx[i];
				if(y1<0||x1<0||y1>=S.size()||x1>=S[0].size()){
					y1 -= dy[i];
					x1 -= dx[i];
					break;
				}
				if(S[y1][x1]!='.')break;
			}
			
			while(true){
				y2 += dy[j];
				x2 += dx[j];
				if(y2<0||x2<0||y2>=S.size()||x2>=S[0].size()){
					y2 -= dy[j];
					x2 -= dx[j];
					break;
				}
				if(S[y2][x2]!='.')break;
			}
			
			if(S[y1][x1] == S[y2][x2] && S[y1][x1]!='.'){
				ans += 2;
				S[y1][x1] = '.';
				S[y2][x2] = '.';
			}
			
		}
	}
}

int main(){
	
	int M,N;
	cin>>M>>N;
	
	vector<string> S;
	{
		bool f = false;
		rep(i,M){
			string s;
			cin>>s;
			if(s==string(N,'.')){
				if(f){
					continue;
				}	
				f=true;
			}
			else{
				f=false;
			}
			S.push_back(s);
		}
	}

	{
		bool F = false;
		vector<string> T(S.size(),"");
		rep(i,N){
			bool f = false;
			rep(j,S.size()){
				if(S[j][i]!='.')f=true;
			}
			if(!f){
				if(F){
					continue;
				}
				F=true;
			}
			else{
				F = false;
			}
			rep(j,S.size()){
				T[j] += S[j][i];
			}
		}
		swap(S,T);
	}
	//cout<<S.size()<<','<<S[0].size()<<endl;
	rep(_,27){
		rep(i,S.size()){
			rep(j,S[i].size()){
				del(S,i,j);
			}
		}
	}
	
	cout<<ans<<endl;
	
    return 0;
}
accuracy: 1
time_ms: 570
memory_kb: 3516
final_rank: 7