Buggy python submission (verdict: Time Limit Exceeded):
# Ford-Fulkerson algorithm
class FordFulkerson:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def dfs(self, v, t, f):
        if v == t:
            return f
        used = self.used
        used[v] = 1
        for e in self.G[v]:
            w, cap, rev = e
            if cap and not used[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        f = INF = 10**9 + 7
        N = self.N
        while f:
            self.used = [0]*N
            f = self.dfs(s, t, INF)
            flow += f
        return flow

H,W=map(int,input().split())
a=[]
for i in range(H):
    a.append(input())

leaf=[]
for i in range(H):
    for j in range(W):
        if a[i][j]=="o":
            leaf.append((i,j))
        elif a[i][j]=="S":
            s=(i,j)
        elif a[i][j]=="T":
            t=(i,j)

inf=300
L=FordFulkerson(H+W+2+2*len(leaf))
for i in range(len(leaf)):
    x,y=leaf[i]
    X,Y=x+1,y+H+1
    index=2*i+H+W+1
    L.add_edge(X,index,inf)
    L.add_edge(Y,index,inf)
    L.add_edge(index,index+1,1)
    L.add_edge(index+1,X,inf)
    L.add_edge(index+1,Y,inf)

i,j=s
L.add_edge(0,i+1,inf)
L.add_edge(0,j+H+1,inf)
i,j=t
L.add_edge(i+1,H+W+2+2*(len(leaf))-1,inf)
L.add_edge(j+H+1,H+W+2+2*(len(leaf))-1,inf)
ans=L.flow(0,H+W+2+2*(len(leaf))-1)
print(ans if inf>ans else -1)

Fixed submission:
# Dinic's algorithm
from collections import deque
class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def bfs(self, s, t):
        self.level = level = [None]*self.N
        deq = deque([s])
        level[s] = 0
        G = self.G
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        return level[t] is not None

    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow

H,W=map(int,input().split())
a=[]
for i in range(H):
    a.append(input())

leaf=[]
for i in range(H):
    for j in range(W):
        if a[i][j]=="o":
            leaf.append((i,j))
        elif a[i][j]=="S":
            s=(i,j)
        elif a[i][j]=="T":
            t=(i,j)

inf=300
L=Dinic(H+W+2+2*len(leaf))
for i in range(len(leaf)):
    x,y=leaf[i]
    X,Y=x+1,y+H+1
    index=2*i+H+W+1
    L.add_edge(X,index,inf)
    L.add_edge(Y,index,inf)
    L.add_edge(index,index+1,1)
    L.add_edge(index+1,X,inf)
    L.add_edge(index+1,Y,inf)

i,j=s
L.add_edge(0,i+1,inf)
L.add_edge(0,j+H+1,inf)
i,j=t
L.add_edge(i+1,H+W+2+2*(len(leaf))-1,inf)
L.add_edge(j+H+1,H+W+2+2*(len(leaf))-1,inf)
ans=L.flow(0,H+W+2+2*(len(leaf))-1)
print(ans if inf>ans else -1)

Buggy python submission (verdict: Time Limit Exceeded):
# Ford-Fulkerson algorithm
class FordFulkerson:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def dfs(self, v, t, f):
        if v == t:
            return f
        used = self.used
        used[v] = 1
        for e in self.G[v]:
            w, cap, rev = e
            if cap and not used[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        f = INF = 10**9 + 7
        N = self.N
        while f:
            self.used = [0]*N
            f = self.dfs(s, t, INF)
            flow += f
        return flow

H,W=map(int,input().split())
a=[]
for i in range(H):
    a.append(input())

leaf=[]
for i in range(H):
    for j in range(W):
        if a[i][j]=="o":
            leaf.append((i,j))
        elif a[i][j]=="S":
            s=(i,j)
        elif a[i][j]=="T":
            t=(i,j)

inf=300
L=FordFulkerson(H+W+2+2*len(leaf))
for i in range(len(leaf)):
    x,y=leaf[i]
    X,Y=x+1,y+H+1
    index=2*i+H+W+1
    L.add_edge(X,index,inf)
    L.add_edge(Y,index,inf)
    L.add_edge(index,index+1,1)
    L.add_edge(index+1,X,inf)
    L.add_edge(index+1,Y,inf)

i,j=s
L.add_edge(0,i+1,inf)
L.add_edge(0,j+H+1,inf)
i,j=t
L.add_edge(i+1,H+W+2+2*(len(leaf))-1,inf)
L.add_edge(j+H+1,H+W+2+2*(len(leaf))-1,inf)
ans=L.flow(0,H+W+2+2*(len(leaf))-1)
print(ans if inf>ans else -1)

Fixed submission:
# Dinic's algorithm
from collections import deque
class Dinic:
    def __init__(self, N):
        self.N = N
        self.G = [[] for i in range(N)]

    def add_edge(self, fr, to, cap):
        forward = [to, cap, None]
        forward[2] = backward = [fr, 0, forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)

    def add_multi_edge(self, v1, v2, cap1, cap2):
        edge1 = [v2, cap1, None]
        edge1[2] = edge2 = [v1, cap2, edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)

    def bfs(self, s, t):
        self.level = level = [None]*self.N
        deq = deque([s])
        level[s] = 0
        G = self.G
        while deq:
            v = deq.popleft()
            lv = level[v] + 1
            for w, cap, _ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    deq.append(w)
        return level[t] is not None

    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
        return 0

    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow

H,W=map(int,input().split())
a=[]
for i in range(H):
    a.append(input())

leaf=[]
for i in range(H):
    for j in range(W):
        if a[i][j]=="o":
            leaf.append((i,j))
        elif a[i][j]=="S":
            s=(i,j)
        elif a[i][j]=="T":
            t=(i,j)

inf=300
L=Dinic(H+W+2+2*len(leaf))
for i in range(len(leaf)):
    x,y=leaf[i]
    X,Y=x+1,y+H+1
    index=2*i+H+W+1
    L.add_edge(X,index,inf)
    L.add_edge(Y,index,inf)
    L.add_edge(index,index+1,1)
    L.add_edge(index+1,X,inf)
    L.add_edge(index+1,Y,inf)

i,j=s
L.add_edge(0,i+1,inf)
L.add_edge(0,j+H+1,inf)
i,j=t
L.add_edge(i+1,H+W+2+2*(len(leaf))-1,inf)
L.add_edge(j+H+1,H+W+2+2*(len(leaf))-1,inf)
ans=L.flow(0,H+W+2+2*(len(leaf))-1)
print(ans if inf>ans else -1)