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>
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) {}
	};
	const T INF;
	int n,m=0;
	std::vector<std::vector<int> > G,Gf;
	std::vector<Edge> E;
	Dinic(int n) : n(n),INF(std::numeric_limits<T>::max()),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|1);
		m+=2;
	}
	bool bfs(int s, int t) {
		Gf.assign(n,0);
		std::queue<int> que; que.emplace(s);
		std::vector<int> dist(n,INF);
		dist[s]=0;
		while(!que.empty()) {
			int i=que.front(); que.pop();
			for(int to:G[i]) {
				if(dist[E[to].b]!=INF || E[to].c==E[to].f) continue;
				dist[E[to].b]=dist[E[to].a]+1;
				que.emplace(E[to].b);
			}
		}
		if(dist[t]==INF) return true;
		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 false;
	}
	T dfs(int i, T max_flow, const int& t) {
		if(i==t || max_flow==0) {
			return max_flow;
		}
		T v_flow=0;
		for(int to:Gf[i]) {
			if(max_flow==0) break;
			T f=dfs(E[to].b,std::min(max_flow,E[to].c-E[to].f),t);
			if(f>0) {
				E[to].f+=f;
				E[to^1].f-=f;
				v_flow+=f;
				max_flow-=f;
			}
		}
		return v_flow;
	}
	T flow(int s, int t) {
		T max_flow=0;
		while(true) {
			if(bfs(s,t)) break;
			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>::bfs(int, int)':
a.cc:55:26: error: no matching function for call to 'std::vector<std::vector<int> >::assign(int&, int)'
   55 |                 Gf.assign(n,0);
      |                 ~~~~~~~~~^~~~~
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:   template argument deduction/substitution failed:
In file included from /usr/include/c++/14/bits/stl_iterator_base_funcs.h:66,
                 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_types.h: In substitution of 'template<class _InIter> using std::_RequireInputIter = std::__enable_if_t<((bool)std::is_convertible<typename std::iterator_traits< <template-parameter-1-1> >::iterator_category, std::input_iterator_tag>::value)> [with _InIter = int]':
/usr/include/c++/14/bits/stl_vector.h:825:9:   required from here
  825 |                typename = std::_RequireInputIter<_InputIterator>>
      |                ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator_base_types.h:252:57: error: no type named 'iterator_category' in 'struct std::iterator_traits<int>'
  252 |                                    input_iterator_tag>::value>;
      |                                                         ^~~~~
/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:47: note:   no known conversion for argument 2 from 'int' to 'const std::vector<std::vector<int> >::value_type&' {aka 'const std::vector<int>&'}
  808 |       assign(size_type __n, const value_type& __val)
      |                             ~~~~~~~~~~~~~~~~~~^~~~~
/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:7: note:   candidate expects 1 argument, 2 provided