package main

import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  buffer := make([]byte, 0)
  for true {
    line, isPrefix, err := reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
    if !isPrefix { break }
  }
  return string(buffer)
}
func NextInt() int {
  n, _ := strconv.Atoi(NextLine())
  return n
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func NextFloat() float64 {
  n, _ := strconv.ParseFloat(NextLine(), 64)
  return n
}
func NextFloatVec() []float64 {
  L := strings.Split(NextLine(), " ")
  N := make([]float64, len(L))
  for i := range N {
    N[i], _ = strconv.ParseFloat(L[i], 64)
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A []int) {
  S := make([]string, len(A))
  for i, a := range A {
    S[i] = strconv.Itoa(a)
  }
  Write(strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

func MaxInt(A ...int) int {
  max := A[0]
  for _, a := range A {
    if max < a { max = a }
  }
  return max
}
func MinInt(A ...int) int {
  min := A[0]
  for _, a := range A {
    if a < min { min = a }
  }
  return min
}

type Edge struct {
  To int
  Capacity int
  Rev int
}
type Graph struct {
  V []int
  E [][]Edge
}
func InitGraph(N int) Graph {
  V := make([]int, N)
  E := make([][]Edge, N)
  for i := range E {
    E[i] = make([]Edge, 0)
  }
  return Graph{V, E}
}
func (G *Graph) AddEdge(From, To, Cap int) {
  (*G).E[From] = append((*G).E[From], Edge{To, Cap, len((*G).E[To])})
  (*G).E[To] = append((*G).E[To], Edge{From, 0, len((*G).E[From]) - 1})
}

func (G *Graph) FordFulkerson(s, t int) int {
  N := len((*G).E)
  P := make([][]int, N)
  Q := make([]int, 0)
  for {
    for i := range P {
      P[i] = []int{-1, -1}
    }
    P[s] = []int{s, -1}
    Q = append(Q, s)
    for 0 < len(Q) {
      q := Q[0]
      Q = Q[1:]
      for i, e := range (*G).E[q] {
        if 0 <= P[e.To][0] || e.Capacity <= 0 { continue }
        P[e.To] = []int{q, i}
        Q = append(Q, e.To)
      }
    }
    if P[t][0] < 0 { break }
    Dst := t
    Src := P[t][0]
    C := (*G).E[P[t][0]][P[t][1]].Capacity
    for Src != s {
      Dst = Src
      Src = P[Src][0]
      C = MinInt((*G).E[P[Dst][0]][P[Dst][1]].Capacity, C)
    }
    Dst = t
    Src = P[t][0]
    for Dst != s {
      e := (*G).E[P[Dst][0]][P[Dst][1]]
      (*G).E[P[Dst][0]][P[Dst][1]].Capacity -= C
      (*G).E[e.To][e.Rev].Capacity += C
      Dst = Src
      Src = P[Src][0]
    }
  }
  F := 0
  for _, e := range (*G).E[s] {
    F += (*G).E[e.To][e.Rev].Capacity
  }
  return F
}

const Inf = 16383

func Solve() {
  HW := NextIntVec()
  H, W := HW[0], HW[1]
  G := InitGraph(H * W + H + W)
  s, t := 0, 0
  for i := 0; i < H; i++ {
    A := NextLine()
    for j, a := range A {
      p := i * W + j
      h := H * W + i
      w := H * W + H + j
      switch a {
      case 'S':
        s = p
        G.AddEdge(p, h, Inf)
        G.AddEdge(p, w, Inf)
      case 'T':
        t = p
        G.AddEdge(h, p, Inf)
        G.AddEdge(w, p, Inf)
      case 'o':
        G.AddEdge(p, h, 1)
        G.AddEdge(h, p, 1)
        G.AddEdge(p, w, 1)
        G.AddEdge(w, p, 1)
      }
    }
  }
  ans := G.FordFulkerson(s, t)
  if Inf <= ans { ans = -1 }
  Write(ans)
  return
}

func main() {
  Solve()
  Output()
}

Measured memory_bytes: 7936