Language: C++

#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
using T=tuple<int,int,char>;
int main(){
    int M,N;
    cin>>M>>N;
    vector<T> V;
    for(int i=0;i<M;i++){
        for(int j=0;j<N;j++){
            char c;
            cin>>c;
            if(c!='.'){
                V.emplace_back(j,i,c);
            }
        }
    }
    vector<int> X,Y;
    for(auto t:V){
        int x,y;
        tie(x,y,ignore)=t;
        X.emplace_back(x);
        Y.emplace_back(y);
        sort(X.begin(), X.end());
        sort(Y.begin(), Y.end());
    }
    X.erase(unique(X.begin(), X.end()),X.end());
    vector<int> XX;
    XX.push_back(X[0]);
    for(int i=1;i<X.size();i++){
        if(X[i-1]+1!=X[i]){
            XX.push_back(X[i-1]+1);
        }
        XX.push_back(X[i]);
    }
    swap(X,XX);
    Y.erase(unique(Y.begin(), Y.end()),Y.end());
    vector<int> YY;
    YY.push_back(Y[0]);
    for(int i=1;i<Y.size();i++){
        if(Y[i-1]+1!=Y[i]){
            YY.push_back(Y[i-1]+1);
        }
        YY.push_back(Y[i]);
    }
    swap(Y,YY);
    int x_max=X.size()-1;
    int y_max=Y.size()-1;
    char board[Y.size()][X.size()];//y,x
    for(int i=0;i<=y_max;i++){
        for(int j=0;j<=x_max;j++){
            board[i][j]='.';
        }
    }
    for(auto &t:V){
        int x,y; char c;
        tie(x,y,c)=t;
        x=lower_bound(X.begin(), X.end(),x)-X.begin();
        y=lower_bound(Y.begin(), Y.end(),y)-Y.begin();
        board[y][x]=c;
    }
    /*
    for(int i=0;i<=y_max;i++){
        for(int j=0;j<=x_max;j++){
            cout<<board[i][j];
        }
        cout<<endl;
    }*/
    bool updated=true;
    int ans=0;
    while(updated){
        updated=false;
        for(int y=0;y<=y_max;y++){
            for(int x=0;x<=x_max;x++){
                if(board[y][x]!='.') continue;
                char up='u',down='d',left='l',right='r';
                int upd,downd,leftd,rightd;
                for(int d=1;x+d<=x_max;d++){
                    if(board[y][x+d]!='.'){
                        right=board[y][x+d];
                        rightd=d;
                        break;
                    }
                }
                for(int d=-1;0<=x+d;d--){
                    if(board[y][x+d]!='.'){
                        left=board[y][x+d];
                        leftd=d;
                        break;
                    }
                }
                for(int d=1;y+d<=y_max;d++){
                    if(board[y+d][x]!='.'){
                        down=board[y+d][x];
                        downd=d;
                        break;
                    }
                }
                for(int d=-1;0<=y+d;d--){
                    if(board[y+d][x]!='.'){
                        up=board[y+d][x];
                        upd=d;
                        break;
                    }
                }
                if(up==down){
                    board[y+upd][x]=board[y+downd][x]='.';
                    ans+=2;
                    updated=true;
                }
                if(up==left){
                    board[y+upd][x]=board[y][x+leftd]='.';
                    ans+=2;
                    updated=true;
                }
                if(up==right){
                    board[y+upd][x]=board[y][x+rightd]='.';
                    ans+=2;
                    updated=true;
                }
                if(down==left){
                    board[y+downd][x]=board[y][x+leftd]='.';
                    ans+=2;
                    updated=true;
                }
                if(down==right){
                    board[y+downd][x]=board[y][x+rightd]='.';
                    ans+=2;
                    updated=true;
                }
                if(left==right){
                    board[y][x+leftd]=board[y][x+rightd]='.';
                    ans+=2;
                    updated=true;
                }
            }
        }
    }
    cout<<ans<<endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:25:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   25 |         sort(X.begin(), X.end());
      |         ^~~~
      |         short
a.cc:28:13: error: 'unique' was not declared in this scope
   28 |     X.erase(unique(X.begin(), X.end()),X.end());
      |             ^~~~~~