Language: C++

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <limits>

#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif

#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
constexpr lnt INF = 2e18;
/*-*/

template <typename T = lnt, T INF = std::numeric_limits<T>::max()>
struct Dinic
{
	struct Edge
	{
		int a,b; T c,f;
		Edge(int a, int b, T c) : a(a),b(b),c(c),f(0) {}
	};
	int n,m;
	std::vector<Edge> E;
	std::vector<std::vector<int> > G,Gf;
	Dinic(int n) : n(n),m(0),G(n),Gf(n) {}
	void add_edge(int i, int j, int c) {
		E.emplace_back(i,j,c);
		E.emplace_back(j,i,0);
		G[i].emplace_back(m++);
		G[j].emplace_back(m++);
	}
	bool bfs(int s, int t) {
		Gf.assign(n,std::vector<int>()jj);
		std::queue<int> que; que.emplace(s);
		std::vector<int> dist(n,-1); dist[s]=0;
		while(!que.empty()) {
			int i=que.front(); que.pop();
			for(int to:G[i]) {
				if(dist[E[to].b]!=-1 || E[to].c==E[to].f) continue;
				dist[E[to].b]=dist[E[to].a]+1;
				que.emplace(E[to].b);
			}
		}
		if(dist[t]==-1) return false;
		for(int i=0;i<m;i++) {
			if(dist[E[i].a]+1!=dist[E[i].b] || E[i].c==E[i].f) continue;
			Gf[E[i].a].emplace_back(i);
		}
		return true;
	}
	T dfs(int i, T a_f, const int& t) {
		if(i==t || a_f==0) {
			return a_f;
		}
		T v_f=0;
		for(int to:Gf[i]) {
			T f=dfs(E[to].b,std::min(a_f-v_f,E[to].c-E[to].f),t);
			E[to].f+=f;
			E[to^1].f-=f;
			v_f+=f;
			if(a_f==v_f) break;
		}
		return v_f;
	}
	T flow(int s, int t) {
		T max_flow=0;
		while(bfs(s,t)) {
			T f = dfs(s,INF,t);
			if(f==0) break;
			max_flow+=f;
		}
		return max_flow;
	}
};

int main() {
	int h,w;
	std::cin >> h >> w;
	std::vector<std::string> g(h);
	rep(i,h) std::cin >> g[i];
	std::vector<std::vector<int> > X(h),Y(w);
	int sx,sy,tx,ty;
	rep(i,h) {
		rep(j,w) {
			if(g[i][j]=='o'||g[i][j]=='S'||g[i][j]=='T') {
				if(g[i][j]=='S') {
					sx=i; sy=j;
				}
				if(g[i][j]=='T') {
					tx=i; ty=j;
				}
				X[i].emplace_back(j);
				Y[j].emplace_back(i);
			}
		}
	}
	if(sx==tx||sy==ty) {
		print(-1);
		return 0;
	}
	auto enc = [&] (int i, int j) {
		return i*w+j;
	};
	debug(sx,sy,tx,ty);
	Dinic<int> flow(2*h*w);
	rep(i,h) {
		for(auto y1:X[i]) for(auto y2:X[i]) {
			if(y1==y2) continue;
			flow.add_edge(enc(i,y1),h*w+enc(i,y2),1);
		}
	}
	rep(i,w) {
		for(auto x1:Y[i]) for(auto x2:Y[i]) {
			if(x1==x2) continue;
			flow.add_edge(enc(x1,i),h*w+enc(x2,i),1);
		}
	}
	rep(i,h) rep(j,w) flow.add_edge(enc(i,j)+h*w,enc(i,j),1);
	print(flow.flow(enc(sx,sy),enc(tx,ty)+h*w));
}


Compiler output:
a.cc: In member function 'bool Dinic<T, INF>::bfs(int, int)':
a.cc:52:47: error: expected ')' before 'jj'
   52 |                 Gf.assign(n,std::vector<int>()jj);
      |                                               ^~
a.cc:52:26: note: to match this '('
   52 |                 Gf.assign(n,std::vector<int>()jj);
      |                          ^