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::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]) {
			if(max_flow==0) break;
			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;
		}
		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:26: error: no matching function for call to 'std::vector<std::vector<int> >::assign(int&)'
   52 |                 Gf.assign(n);
      |                 ~~~~~~~~~^~~
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_vector.h:828:9: note: candidate: 'template<class _InputIterator, class> void std::vector<_Tp, _Alloc>::assign(_InputIterator, _InputIterator) [with <template-parameter-2-2> = _InputIterator; _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >]'
  828 |         assign(_InputIterator __first, _InputIterator __last)
      |         ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:828:9: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_vector.h:808:7: note: candidate: 'void std::vector<_Tp, _Alloc>::assign(size_type, const value_type&) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >; size_type = long unsigned int; value_type = std::vector<int>]'
  808 |       assign(size_type __n, const value_type& __val)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:808:7: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/14/bits/stl_vector.h:855:7: note: candidate: 'void std::vector<_Tp, _Alloc>::assign(std::initializer_list<_Tp>) [with _Tp = std::vector<int>; _Alloc = std::allocator<std::vector<int> >]'
  855 |       assign(initializer_list<value_type> __l)
      |       ^~~~~~
/usr/include/c++/14/bits/stl_vector.h:855:43: note:   no known conversion for argument 1 from 'int' to 'std::initializer_list<std::vector<int> >'
  855 |       assign(initializer_list<value_type> __l)
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
a.cc: In member function 'T Dinic<T, INF>::dfs(int, T, const int&)':
a.cc:76:28: error: 'max_flow' was not declared in this scope
   76 |                         if(max_flow==0) break;
      |                            ^~~~~~~~