Language: C++

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#include <map>
#include <set>
#include <utility>
#include <vector>
#include <queue>
#include <cmath>
#define read(X) scanf("%d",&X);
#define readLL(X) cin>>X;
#define readN(N,a) for(int i=0;i<N;i++)scanf("%lld",&a[i]);
#define LL long long
#define FOR(x,a,b) for(int x=a;x<b;x++)
#define pii pair<int,int>
using namespace std;

const int MAXN = 200002; /* max number of vertices - adjust */
const int MAXM = 200002; /* max number of edges - adjust */
const int INF = 1<<30;

int flow;
int bottleneck;
int n, m, e;
int s, t;
int x, y, w;
int p[MAXN], edge[MAXN];
int last[MAXN];
int prev[MAXM];
int to[MAXM];
int c[MAXM], f[MAXM];

/* add directed edge x->y with capacity w */
void add(int x, int y, int w)
{   prev[e] = last[x];
    last[x] = e;
    to[e] = y;
    c[e] = w;
    f[e] = 0;
    e++;
    prev[e] = last[y];
    last[y] = e;
    to[e] = x;
    c[e] = 0;
    f[e] = 0;
    e++;
}

/* find augmenting path in residual graph */
bool path(int u, int v)
{   queue<int> Q;
    int C, F;
    
    for(int i = 0; i < n; i++) p[i] = -1;
    Q.push(u);
    p[u] = u;
    while(!Q.empty())
    {   
        x = Q.front(), Q.pop();
        //cout<<x<<endl;
        
        if(x == v) return true;
        for(int i=last[x]; i!=-1; i=prev[i])
        {   
            //cout<<i<<endl;
            y = to[i];
            F = f[i];
            C = c[i];
            if(p[y] == -1 && C > F)
            {   p[y] = x;
                edge[y] = i;
                Q.push(y);
            }
        }
    }
    return false;
}

/* send flow through augmenting path */
int send(int u, int v)
{   bottleneck = INF;
    int x, y, index, sym;
    y = v;

    while(p[y] != y)
    {   x = p[y];
        index = edge[y];
        bottleneck = (bottleneck > c[index]-f[index] ? c[index]-f[index] : bottleneck);
        y = x;
    }

    y = v;
    while(p[y] != y)
    {   x = p[y];
        index = edge[y];
        f[index] += bottleneck;
        f[index^1] -= bottleneck;
        y = x;
    }

    return bottleneck;
}

int N,M;
char lho[111][111];
int nid[111][111];
int main(){
    read(N);
    read(M);
    n,m=0;

    /* build graph */
    memset(last, -1, sizeof(last));

    FOR(i,0,N){
        scanf("%s",lho[i]);
    }
    //cout<<"dah"<<endl;

    FOR(i,0,N)
        FOR(j,0,M){
            if (lho[i][j]=='.') continue;
            if (lho[i][j]=='S'){
                s = n+1;
                
            }
            
            if (lho[i][j]=='T'){
                t = n;
                
            }
            if (lho[i][j]!='.'){
                n+=2;
                add(n-2, n-1,1); m++;
                nid[i][j] = n-2;
            }

        }

    //cout<<"wut "<<n<<" "<<m<<endl;
    FOR(i,0,N)
        FOR(j,0,M){
            if (lho[i][j] != '.'){
                FOR(k,0,N){
                    if (i==k) continue;
                    if (lho[k][j]!='.'){
                        m++; add(nid[i][j] + 1, nid[k][j], 1);
                    }
                    if (lho[i][j]=='S' && lho[k][j]=='T') {
                        cout<<-1<<endl;
                        return 0;
                    }
                }

                FOR(k,0,M){
                    if (j==k) continue;
                    if (lho[i][k]!='.'){
                        m++; add(nid[i][j] + 1, nid[i][k], 1);
                    }
                    if (lho[i][j]=='S' && lho[i][k]=='T') {
                        cout<<-1<<endl;
                        return 0;
                    }
                }
            }
        }


    //cout<<"wut "<<s<<" "<<t<<endl;
    flow = 0;
    while(path(s, t)){
        //cout<<"hah "<<flow<<endl;
        flow += send(s, t);
    }
    printf("%d\n", flow);
    
    return 0;
}


Compiler output:
a.cc: In function 'void add(int, int, int)':
a.cc:37:5: error: reference to 'prev' is ambiguous
   37 | {   prev[e] = last[x];
      |     ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:5: note:                 'int prev [200002]'
   31 | int prev[MAXM];
      |     ^~~~
a.cc:43:5: error: reference to 'prev' is ambiguous
   43 |     prev[e] = last[y];
      |     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:5: note:                 'int prev [200002]'
   31 | int prev[MAXM];
      |     ^~~~
a.cc: In function 'bool path(int, int)':
a.cc:65:37: error: reference to 'prev' is ambiguous
   65 |         for(int i=last[x]; i!=-1; i=prev[i])
      |                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:31:5: note:                 'int prev [200002]'
   31 | int prev[MAXM];
      |     ^~~~