from scipy.sparse.csgraph import *;from scipy.sparse import *;H,W,*S=open(0).read().split();H,W=int(H),int(W);m=H*W;A=[]

for i in range(m):

 h,w=divmod(i,W);A=[m*2,h,m*2,w+m]+A if S[h][w]=='S' else [h,m*3,w+m,m*3]+A if S[h][w]=='T' else A+[h,w+m,w+m,h] if S[h][w]=='o' else A

f=maximum_flow(csr_matrix(([9**9]*4+[1]*(len(A)//2-4),(A[::2], A[1::2])),shape=(m*4,m*4)),m*2,m*3).flow_value;print((f if f<9**9 else -1))
from scipy.sparse.csgraph import *;from scipy.sparse import *;H,W,*S=open(0).read().split();H,W=int(H),int(W);m=H*W;A=[]

for i in range(m):

 h,w=divmod(i,W)

 if S[h][w]=='S':A=[m*2,h,m*2,w+m]+A

 elif S[h][w]=='T':A=[h,m*3,w+m,m*3]+A

 elif S[h][w]=='o':A+=[h,w+m,w+m,h]

f=maximum_flow(csr_matrix(([9**9]*4+[1]*(len(A)//2-4),(A[::2], A[1::2])),shape=(m*4,m*4)),m*2,m*3).flow_value;print((f if f<9**9 else -1))
from scipy.sparse.csgraph import *
from scipy.sparse import *

H, W, *S = open(0).read().split()
H, W = int(H), int(W)
m = H * W
A = []
for i in range(m):
    h, w = divmod(i, W)
    A = (
        [m * 2, h, m * 2, w + m] + A
        if S[h][w] == "S"
        else [h, m * 3, w + m, m * 3] + A
        if S[h][w] == "T"
        else A + [h, w + m, w + m, h]
        if S[h][w] == "o"
        else A
    )
f = maximum_flow(
    csr_matrix(
        ([9**9] * 4 + [1] * (len(A) // 2 - 4), (A[::2], A[1::2])),
        shape=(m * 4, m * 4),
    ),
    m * 2,
    m * 3,
).flow_value
print((f if f < 9**9 else -1))

from scipy.sparse.csgraph import *
from scipy.sparse import *

H, W, *S = open(0).read().split()
H, W = int(H), int(W)
m = H * W
A = []
for i in range(m):
    h, w = divmod(i, W)
    if S[h][w] == "S":
        A = [m * 2, h, m * 2, w + m] + A
    elif S[h][w] == "T":
        A = [h, m * 3, w + m, m * 3] + A
    elif S[h][w] == "o":
        A += [h, w + m, w + m, h]
f = maximum_flow(
    csr_matrix(
        ([9**9] * 4 + [1] * (len(A) // 2 - 4), (A[::2], A[1::2])),
        shape=(m * 4, m * 4),
    ),
    m * 2,
    m * 3,
).flow_value
print((f if f < 9**9 else -1))

-    A = ( -        [m * 2, h, m * 2, w + m] + A -        if S[h][w] == "S" -        else [h, m * 3, w + m, m * 3] + A -        if S[h][w] == "T" -        else A + [h, w + m, w + m, h] -        if S[h][w] == "o" -        else A -    ) +    if S[h][w] == "S": +        A = [m * 2, h, m * 2, w + m] + A +    elif S[h][w] == "T": +        A = [h, m * 3, w + m, m * 3] + A +    elif S[h][w] == "o": +        A += [h, w + m, w + m, h]