// Slower C++ solution:
#include<bits/stdc++.h>

#define LL long long

#define MEM(x,y) memset(x,y,sizeof(x))

#define MOD(x) ((x)%mod)

#define mod 1000000007

#define pb push_back

#define STREAM_FAST ios::sync_with_stdio(false)

 

using namespace std;

const int maxn = 2e5+7;

struct node {int v,flow,nxt;}e[maxn];

int tot=1,head[maxn],dep[maxn];

// tot starting from 1

void add(int u,int v,int w)

{

    ++tot;

    e[tot].v=v;e[tot].flow=w;e[tot].nxt=head[u];

    head[u]=tot;

}

 

bool bfs(int S,int T)

{

    MEM(dep,0);

    dep[S]=1;

    queue<int>q;

    q.push(S);

    while (!q.empty())

    {

        int u=q.front();q.pop();

        for (int i=head[u];i;i=e[i].nxt)

        {

            int v=e[i].v;

            if (e[i].flow && !dep[v])

            {

                dep[v]=dep[u]+1;

                q.push(e[i].v);

            }

        }

    }

    return dep[T];

}

 

int aim;

int dfs(int now,int flow)

{

    if (now==aim) return flow;

    int f=0;

    for (int i=head[now];i;i=e[i].nxt)

    {

        int v=e[i].v;

        if (e[i].flow && dep[v]==dep[now]+1)

        {

            int x=dfs(v,min(flow,e[i].flow));

            e[i].flow-=x;e[i^1].flow+=x;flow-=x;f+=x;

        }

    }

    if (!f) dep[now]=-2;

    return f;

}

 

int maxflow(int S,int T)

{

    aim=T;int res=0;

    while (bfs(S,T)) res+=dfs(S,1<<30);

    return res;

}

 

int main()

{

    int n,m;scanf("%d%d",&n,&m);

    int S=1000,T=1001;

    

    for (int i=1;i<=n;i++)

    {

        for (int j=1;j<=m;j++)

        {

            char c;scanf(" %c",&c);

            if (c=='o') 

            {

                add(i,j+n,1);add(j+n,i,1);

            }

            else if (c=='S')

            {

                add(S,i,1e8);add(i,S,0);

                add(S,j+n,1e8);add(j+n,S,0);

            }

            else if (c=='T')

            {

                add(i,T,1e8);add(T,i,0);

                add(j+n,T,1e8);add(T,j+n,0);

            }

        }

    }

    

    int ans=maxflow(S,T);

    printf("%d\n",ans>=1e8?-1:ans);

    

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.7492963786486768x):
#include <bits/stdc++.h>



using namespace std;



struct FlowEdge

{

	int u, v;

	long long c, f = 0;

	FlowEdge(){}

	FlowEdge(int _u,int _v,long long _c)

	{

		u = _u; v = _v; c = _c;

	}

};



struct Template_Dinic

{

	const long long flow_INF = 1e18;	

	vector<FlowEdge> Edges;

	vector<vector<int>> AdjList;

	int n, s, t, m = 0;

	vector<int> d, ptr;

	queue<int> q;



	void init(int _n,int _s,int _t)

	{

		n = _n, s = _s, t = _t;

		AdjList.resize(n + 1); 

		d.resize(n + 1);

		ptr.resize(n + 1);

	}



	void add_edge(int u,int v,long long c)

	{

		Edges.push_back(FlowEdge(u,v,c));

		Edges.push_back(FlowEdge(v,u,c));

		AdjList[u].push_back(m);

		AdjList[v].push_back(m + 1);

		m += 2;

	}



	bool bfs()

	{

		while(!q.empty())

		{

			int u = q.front();

			q.pop();

			for (int i=0;i<AdjList[u].size();i++)

			{

				int id = AdjList[u][i];

				if (Edges[id].f < Edges[id].c && d[Edges[id].v] == -1)

				{

					d[Edges[id].v] = d[u] + 1;

					q.push(Edges[id].v);

				}

			}

		}

		return d[t] != -1;

	}



	long long dfs(int u, long long cur)

	{

		if (cur == 0) return 0;

		if (u == t) return cur;

		for (int& cid = ptr[u];cid < AdjList[u].size();cid++)

		{

			int id = AdjList[u][cid];

			int v = Edges[id].v;

			if (d[u] + 1 == d[v] && Edges[id].f < Edges[id].c)

			{

				long long nxt = dfs(v, min(cur,Edges[id].c - Edges[id].f));

				if (!nxt) continue;

				Edges[id].f += nxt;

				Edges[id ^ 1].f -= nxt;

				return nxt;

			}

		}

		return 0;

	}



	long long flow()

	{

		long long res = 0;

		while(1)

		{

			for (int i=0;i<d.size();i++) d[i] = -1;

			d[s] = 0;

			q.push(s);

			if (!bfs())

				break;

			for (int i=0;i<ptr.size();i++) ptr[i] = 0;

			while(long long cur = dfs(s, flow_INF))

				res += cur; 	

		}

		return res;

	}

}Dinic;



int h,w;



int main()

{

	ios::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);



	cin >> h >> w;

	Dinic.init(500,0,201);

	for (int i=1;i<=h;i++)

		for (int j=1;j<=w;j++)	

		{

			char c; cin >> c;

			if (c == 'S')

			{

				Dinic.add_edge(0,i,1e9);

				Dinic.add_edge(0,j+100,1e9);	

			}

			else if (c == 'T')

			{

				Dinic.add_edge(i,201,1e9);

				Dinic.add_edge(j+100,201,1e9);

			}

			else if (c == 'o')

				Dinic.add_edge(i,j+100,1);

		}

	long long res = Dinic.flow();

	if (res >= 1e9) cout << -1;

	else cout << res;

}