import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



from collections import defaultdict



H,W = list(map(int,readline().split()))

A = [line.rstrip().decode('utf-8') for line in readlines()]



source = 0

sink = H+W+1



graph = [defaultdict(int) for _ in range(H+W+2)]



INF = 10 ** 18

for h in range(1,H+1):

    for w,ox in enumerate(A[h-1],1):

        if ox == 'x':

            continue

        elif ox == 'o':

            graph[h][H+w] = 1

            graph[H+w][h] = 1

        elif ox == 'S':

            graph[source][h] = INF

            graph[h][source] = INF

            graph[source][H+w] = INF

            graph[H+w][source] = INF

        elif ox == 'T':

            graph[sink][h] = INF

            graph[h][sink] = INF

            graph[sink][H+w] = INF

            graph[H+w][sink] = INF





class Dinic():

    def __init__(self,graph,V,source,sink):

        self.graph = graph

        self.sink = sink

        self.source = source

        self.V = V

#        self.compress()

        self.N = len(V)

    

    def compress(self):

        self.N = len(self.V)

        v_to_i = {x:i for i,x in enumerate(self.V)}

        self.sink = v_to_i[self.sink]

        self.source = v_to_i[self.source]

        g = [dict() for _ in range(self.N)]

        for v,e in list(self.graph.items()):

            vn = v_to_i[v]

            g[vn] = {v_to_i[w]:c for w,c in list(e.items())}

        self.graph = g

        

    def bfs(self):

        level = [0]*self.N

        q = [self.source]

        level[self.source] = 1

        d = 1

        while q:

            if level[self.sink]:

                break

            qq = []

            d += 1

            for v in q:

                for w,cap in list(self.graph[v].items()):

                    if cap == 0:

                        continue

                    if level[w]:

                        continue

                    level[w] = d

                    qq.append(w)

            q = qq

        self.level = level

        

    def dfs(self,v,f):

        if v == self.sink:

            return f

        for w,cap in self.itr[v]:

            if cap == 0 or self.level[w] != self.level[v] + 1:

                continue

            d = self.dfs(w,min(f,cap))

            if d:

                self.graph[v][w] -= d

                self.graph[w][v] += d

                return d

        return 0

    

    def max_flow(self):

        INF = 10**18

        flow = 0

        while True:

            self.bfs()

            if self.level[self.sink] == 0:

                break

            self.itr = [iter(list(e.items())) for e in self.graph]

            while True:

                f = self.dfs(self.source,INF)

                if f == 0:

                    break

                flow += f

        return flow



answer = Dinic(graph=graph,V=list(range(H+W+2)),source=0,sink=H+W+1).max_flow()

if answer >= INF:

    answer = -1

print(answer)
import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



from collections import deque



H,W = list(map(int,readline().split()))

A = [line.rstrip().decode('utf-8') for line in readlines()]



class Dinic:

    def __init__(self, N, source, sink):

        self.N = N

        self.G = [[] for _ in range(N)]

        self.source = source

        self.sink = sink



    def add_edge(self, fr, to, cap):

        n1 = len(self.G[fr])

        n2 = len(self.G[to])

        self.G[fr].append([to, cap, n2])

        self.G[to].append([fr, 0, n1]) # 逆辺を cap 0 で追加

        

    def bfs(self):

        level = [0] * self.N

        G = self.G; source = self.source; sink = self.sink

        q = deque([source])

        level[source] = 1

        pop = q.popleft; append = q.append

        while q:

            v = pop()

            lv = level[v] + 1

            for to, cap, rev in G[v]:

                if not cap:

                    continue

                if level[to]:

                    continue

                level[to] = lv

                if to == sink:

                    self.level = level

                    return

                append(to)

        self.level = level

        

    def dfs(self,v,f):

        if v == self.sink:

            return f

        G = self.G

        prog = self.progress

        level = self.level

        lv = level[v]

        E = G[v]

        for i in range(prog[v],len(E)):

            to, cap, rev = E[i]

            prog[v] = i

            if not cap:

                continue

            if level[to] <= lv:

                continue

            x = f if f < cap else cap

            ff = self.dfs(to, x)

            if ff:

                E[i][1] -= ff

                G[to][rev][1] += ff

                return ff

        return 0

    

    def max_flow(self):

        INF = 10**18

        flow = 0

        while True:

            self.bfs()

            if not self.level[self.sink]:

                return flow

            self.progress = [0] * self.N

            while True:

                f = self.dfs(self.source, INF)

                if not f:

                    break

                flow += f

        return flow



source = 0

sink = H+W+1



dinic = Dinic(H+W+2, source, sink)

add = dinic.add_edge



INF = 10 ** 18

for h in range(1,H+1):

    for w,ox in enumerate(A[h-1],1):

        if ox == 'x':

            continue

        elif ox == 'o':

            add(h,H+w,1)

            add(H+w,h,1)

        elif ox == 'S':

            add(source,h,INF)

            add(h,source,INF)

            add(source,H+w,INF)

            add(H+w,source,INF)

        elif ox == 'T':

            add(sink,h,INF)

            add(h,sink,INF)

            add(sink,H+w,INF)

            add(H+w,sink,INF)



f = dinic.max_flow()

answer = f if f < INF else -1

print(answer)
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import defaultdict

H, W = list(map(int, readline().split()))
A = [line.rstrip().decode("utf-8") for line in readlines()]
source = 0
sink = H + W + 1
graph = [defaultdict(int) for _ in range(H + W + 2)]
INF = 10**18
for h in range(1, H + 1):
    for w, ox in enumerate(A[h - 1], 1):
        if ox == "x":
            continue
        elif ox == "o":
            graph[h][H + w] = 1
            graph[H + w][h] = 1
        elif ox == "S":
            graph[source][h] = INF
            graph[h][source] = INF
            graph[source][H + w] = INF
            graph[H + w][source] = INF
        elif ox == "T":
            graph[sink][h] = INF
            graph[h][sink] = INF
            graph[sink][H + w] = INF
            graph[H + w][sink] = INF


class Dinic:
    def __init__(self, graph, V, source, sink):
        self.graph = graph
        self.sink = sink
        self.source = source
        self.V = V
        #        self.compress()
        self.N = len(V)

    def compress(self):
        self.N = len(self.V)
        v_to_i = {x: i for i, x in enumerate(self.V)}
        self.sink = v_to_i[self.sink]
        self.source = v_to_i[self.source]
        g = [dict() for _ in range(self.N)]
        for v, e in list(self.graph.items()):
            vn = v_to_i[v]
            g[vn] = {v_to_i[w]: c for w, c in list(e.items())}
        self.graph = g

    def bfs(self):
        level = [0] * self.N
        q = [self.source]
        level[self.source] = 1
        d = 1
        while q:
            if level[self.sink]:
                break
            qq = []
            d += 1
            for v in q:
                for w, cap in list(self.graph[v].items()):
                    if cap == 0:
                        continue
                    if level[w]:
                        continue
                    level[w] = d
                    qq.append(w)
            q = qq
        self.level = level

    def dfs(self, v, f):
        if v == self.sink:
            return f
        for w, cap in self.itr[v]:
            if cap == 0 or self.level[w] != self.level[v] + 1:
                continue
            d = self.dfs(w, min(f, cap))
            if d:
                self.graph[v][w] -= d
                self.graph[w][v] += d
                return d
        return 0

    def max_flow(self):
        INF = 10**18
        flow = 0
        while True:
            self.bfs()
            if self.level[self.sink] == 0:
                break
            self.itr = [iter(list(e.items())) for e in self.graph]
            while True:
                f = self.dfs(self.source, INF)
                if f == 0:
                    break
                flow += f
        return flow


answer = Dinic(
    graph=graph, V=list(range(H + W + 2)), source=0, sink=H + W + 1
).max_flow()
if answer >= INF:
    answer = -1
print(answer)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

H, W = list(map(int, readline().split()))
A = [line.rstrip().decode("utf-8") for line in readlines()]


class Dinic:
    def __init__(self, N, source, sink):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.source = source
        self.sink = sink

    def add_edge(self, fr, to, cap):
        n1 = len(self.G[fr])
        n2 = len(self.G[to])
        self.G[fr].append([to, cap, n2])
        self.G[to].append([fr, 0, n1])  # 逆辺を cap 0 で追加

    def bfs(self):
        level = [0] * self.N
        G = self.G
        source = self.source
        sink = self.sink
        q = deque([source])
        level[source] = 1
        pop = q.popleft
        append = q.append
        while q:
            v = pop()
            lv = level[v] + 1
            for to, cap, rev in G[v]:
                if not cap:
                    continue
                if level[to]:
                    continue
                level[to] = lv
                if to == sink:
                    self.level = level
                    return
                append(to)
        self.level = level

    def dfs(self, v, f):
        if v == self.sink:
            return f
        G = self.G
        prog = self.progress
        level = self.level
        lv = level[v]
        E = G[v]
        for i in range(prog[v], len(E)):
            to, cap, rev = E[i]
            prog[v] = i
            if not cap:
                continue
            if level[to] <= lv:
                continue
            x = f if f < cap else cap
            ff = self.dfs(to, x)
            if ff:
                E[i][1] -= ff
                G[to][rev][1] += ff
                return ff
        return 0

    def max_flow(self):
        INF = 10**18
        flow = 0
        while True:
            self.bfs()
            if not self.level[self.sink]:
                return flow
            self.progress = [0] * self.N
            while True:
                f = self.dfs(self.source, INF)
                if not f:
                    break
                flow += f
        return flow


source = 0
sink = H + W + 1
dinic = Dinic(H + W + 2, source, sink)
add = dinic.add_edge
INF = 10**18
for h in range(1, H + 1):
    for w, ox in enumerate(A[h - 1], 1):
        if ox == "x":
            continue
        elif ox == "o":
            add(h, H + w, 1)
            add(H + w, h, 1)
        elif ox == "S":
            add(source, h, INF)
            add(h, source, INF)
            add(source, H + w, INF)
            add(H + w, source, INF)
        elif ox == "T":
            add(sink, h, INF)
            add(h, sink, INF)
            add(sink, H + w, INF)
            add(H + w, sink, INF)
f = dinic.max_flow()
answer = f if f < INF else -1
print(answer)

-from collections import defaultdict +from collections import deque -source = 0 -sink = H + W + 1 -graph = [defaultdict(int) for _ in range(H + W + 2)] -INF = 10**18 -for h in range(1, H + 1): -    for w, ox in enumerate(A[h - 1], 1): -        if ox == "x": -            continue -        elif ox == "o": -            graph[h][H + w] = 1 -            graph[H + w][h] = 1 -        elif ox == "S": -            graph[source][h] = INF -            graph[h][source] = INF -            graph[source][H + w] = INF -            graph[H + w][source] = INF -        elif ox == "T": -            graph[sink][h] = INF -            graph[h][sink] = INF -            graph[sink][H + w] = INF -            graph[H + w][sink] = INF -    def __init__(self, graph, V, source, sink): -        self.graph = graph +    def __init__(self, N, source, sink): +        self.N = N +        self.G = [[] for _ in range(N)] +        self.source = source -        self.source = source -        self.V = V -        #        self.compress() -        self.N = len(V) -    def compress(self): -        self.N = len(self.V) -        v_to_i = {x: i for i, x in enumerate(self.V)} -        self.sink = v_to_i[self.sink] -        self.source = v_to_i[self.source] -        g = [dict() for _ in range(self.N)] -        for v, e in list(self.graph.items()): -            vn = v_to_i[v] -            g[vn] = {v_to_i[w]: c for w, c in list(e.items())} -        self.graph = g +    def add_edge(self, fr, to, cap): +        n1 = len(self.G[fr]) +        n2 = len(self.G[to]) +        self.G[fr].append([to, cap, n2]) +        self.G[to].append([fr, 0, n1])  # 逆辺を cap 0 で追加 -        q = [self.source] -        level[self.source] = 1 -        d = 1 +        G = self.G +        source = self.source +        sink = self.sink +        q = deque([source]) +        level[source] = 1 +        pop = q.popleft +        append = q.append -            if level[self.sink]: -                break -            qq = [] -            d += 1 -            for v in q: -                for w, cap in list(self.graph[v].items()): -                    if cap == 0: -                        continue -                    if level[w]: -                        continue -                    level[w] = d -                    qq.append(w) -            q = qq +            v = pop() +            lv = level[v] + 1 +            for to, cap, rev in G[v]: +                if not cap: +                    continue +                if level[to]: +                    continue +                level[to] = lv +                if to == sink: +                    self.level = level +                    return +                append(to) -        for w, cap in self.itr[v]: -            if cap == 0 or self.level[w] != self.level[v] + 1: +        G = self.G +        prog = self.progress +        level = self.level +        lv = level[v] +        E = G[v] +        for i in range(prog[v], len(E)): +            to, cap, rev = E[i] +            prog[v] = i +            if not cap: -            d = self.dfs(w, min(f, cap)) -            if d: -                self.graph[v][w] -= d -                self.graph[w][v] += d -                return d +            if level[to] <= lv: +                continue +            x = f if f < cap else cap +            ff = self.dfs(to, x) +            if ff: +                E[i][1] -= ff +                G[to][rev][1] += ff +                return ff -            if self.level[self.sink] == 0: -                break -            self.itr = [iter(list(e.items())) for e in self.graph] +            if not self.level[self.sink]: +                return flow +            self.progress = [0] * self.N -                if f == 0: +                if not f: -answer = Dinic( -    graph=graph, V=list(range(H + W + 2)), source=0, sink=H + W + 1 -).max_flow() -if answer >= INF: -    answer = -1 +source = 0 +sink = H + W + 1 +dinic = Dinic(H + W + 2, source, sink) +add = dinic.add_edge +INF = 10**18 +for h in range(1, H + 1): +    for w, ox in enumerate(A[h - 1], 1): +        if ox == "x": +            continue +        elif ox == "o": +            add(h, H + w, 1) +            add(H + w, h, 1) +        elif ox == "S": +            add(source, h, INF) +            add(h, source, INF) +            add(source, H + w, INF) +            add(H + w, source, INF) +        elif ox == "T": +            add(sink, h, INF) +            add(h, sink, INF) +            add(sink, H + w, INF) +            add(H + w, sink, INF) +f = dinic.max_flow() +answer = f if f < INF else -1