Problem:
Score : 800 points

Problem Statement

There is a pond with a rectangular shape.
The pond is divided into a grid with H rows and W columns of squares.
We will denote the square at the i-th row from the top and j-th column from the left by (i,\ j).

Some of the squares in the pond contains a lotus leaf floating on the water.
On one of those leaves, S, there is a frog trying to get to another leaf T.
The state of square (i,\ j) is given to you by a character a_{ij}, as follows:

. : A square without a leaf.

o : A square with a leaf floating on the water.

S : A square with the leaf S.

T : A square with the leaf T.

The frog will repeatedly perform the following action to get to the leaf T: "jump to a leaf that is in the same row or the same column as the leaf where the frog is currently located."

Snuke is trying to remove some of the leaves, other than S and T, so that the frog cannot get to the leaf T.
Determine whether this objective is achievable. If it is achievable, find the minimum necessary number of leaves to remove.

Constraints

2 ≤ H, W ≤ 100

a_{ij} is ., o, S or T.

There is exactly one S among a_{ij}.

There is exactly one T among a_{ij}.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

If the objective is achievable, print the minimum necessary number of leaves to remove.
Otherwise, print -1 instead.

Sample Input 1

3 3
S.o
.o.
o.T

Sample Output 1

2

Remove the upper-right and lower-left leaves.

Sample Input 2

3 4
S...
.oo.
...T

Sample Output 2

0

Sample Input 3

4 3
.S.
.o.
.o.
.T.

Sample Output 3

-1

Sample Input 4

10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo

Sample Output 4

5

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>

typedef int32_t flow_type;

const flow_type flow_inf = 200;

typedef struct flow_edge {
  int32_t vertex;
  int32_t next;
  flow_type capacity;
} flow_edge;

typedef struct maxFlowGraph {
  flow_edge *edge;
  int32_t *start;
  int32_t vertex_num;
  int32_t pointer;
  int32_t edge_length;
} graph;

graph* new_graph (const int32_t vertex_num) {
  graph *g = (graph *) calloc (1, sizeof (graph));
  g->vertex_num = vertex_num;
  const int32_t initial_length = 4;
  g->edge = (flow_edge *) calloc (initial_length, sizeof (flow_edge));
  g->start = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  g->pointer = 0;
  g->edge_length = initial_length;
  for (int32_t i = 0; i < vertex_num; ++i) {
    g->start[i] = -1;
  }
  return g;
}

void free_graph (graph * const g) {
  free (g->edge);
  free (g->start);
  free (g);
}

void add_edge (graph * const g, const int32_t from, const int32_t to, const flow_type capa) {
  if (g->pointer == g->edge_length) {
    g->edge_length *= 2;
    g->edge = (flow_edge *) realloc (g->edge, sizeof (flow_edge) * g->edge_length);
  }
  const int32_t p = g->pointer;
  g->edge[p] = (flow_edge) {to, g->start[from], capa};
  g->start[from] = p;
  g->edge[p + 1] = (flow_edge) {from, g->start[to], 0};
  g->start[to] = p + 1;
  g->pointer += 2;
}

flow_type dinic_dfs (const int32_t v, graph * const g, const int32_t dst, const int32_t * const level, int32_t * const iter, flow_type e) {
  if (v == dst) return e;
  flow_type sum = 0;
  for (int32_t p = iter[v]; p != -1; p = g->edge[p].next, iter[v] = p) {
    const int32_t u = g->edge[p].vertex;
    const flow_type capa = g->edge[p].capacity;
    if (level[u] <= level[v] || capa <= 0) continue;
    const flow_type f = dinic_dfs (u, g, dst, level, iter, capa < e ? capa : e);
    if (f > 0) {
      g->edge[p].capacity -= f;
      g->edge[p ^ 1].capacity += f;
      sum += f;
      e -= f;
      if (e <= 0) return sum;
    }
  }
  return sum;
}

void dinic_bfs (const graph *g, const int32_t src, const int32_t dst, int32_t * const level, int32_t * const queue, const int32_t rev) {
  memset (level, 0, sizeof (int32_t) * g->vertex_num);
  level[src] = 1;
  int32_t front = 0;
  int32_t last = 0;
  queue[last++] = src;
  while (front < last && level[dst] == 0) {
    const int32_t v = queue[front++];
    for (int32_t p = g->start[v]; p != -1; p = g->edge[p].next) {
      const int32_t u = g->edge[p].vertex;
      if (g->edge[p^rev].capacity > 0 && level[u] == 0) {
	level[u] = level[v] + 1;
	queue[last++] = u;
      }
    }
  }
}

flow_type dinic (graph * const g, const int32_t src, const int32_t dst) {
  const int32_t vertex_num = g->vertex_num;
  int32_t * const level = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  int32_t * const queue = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  int32_t * const rev_level = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  int32_t * const iter  = (int32_t *) calloc (vertex_num, sizeof (int32_t));
  flow_type flow = 0;
  while (1) {
    dinic_bfs (g, src, dst, level, queue, 0);
    if (level[dst] == 0) break;
    dinic_bfs (g, dst, src, rev_level, queue, 1);
    for (int32_t i = 0; i < vertex_num; ++i) {
      if (level[i] + rev_level[i] != level[dst] + 1) {
	level[i] = 0;
      }
    }
    memcpy (iter, g->start, sizeof (int32_t) * vertex_num);
    while (1) {
      flow_type f = dinic_dfs (src, g, dst, level, iter, flow_inf);
      if (f <= 0) break;
      flow += f;
    }
  }
  free (level);
  free (queue);
  free (iter);
  return flow;
}

typedef int32_t i32;

void run (void) {
  i32 h, w;
  scanf ("%" SCNi32 "%" SCNi32, &h, &w);
  char *a = (char *) calloc (h * w + 1, sizeof (char));
  for (i32 i = 0; i < h; ++i) {
    scanf ("%s", a + i * w);
  }
  graph *g = new_graph (2 * h * w);
  i32 src = -1;
  i32 dst = -1;
  for (i32 i = 0; i < h; ++i) {
    for (i32 j = 0; j < w; ++j) {
      if (a[i * w + j] == '.') continue;
      if (a[i * w + j] == 'S') {
        src = i * w + j;
		add_edge (g, h * w + i * w + j, i * w + j, flow_inf);
      } else if (a[i * w + j] == 'T') {
        dst = i * w + j;
		add_edge (g, h * w + i * w + j, i * w + j, flow_inf);
      } else {
		add_edge (g, h * w + i * w + j, i * w + j, 1);
      }
      for (i32 k = 0; k < h; ++k) {
		if (k == i || a[k * w + j] == '.') continue;
		add_edge (g, i * w + j, h * w + k * w + j, flow_inf);
      }
      for (i32 k = 0; k < w; ++k) {
		if (k == j || a[i * w + k] == '.') continue;
		add_edge (g, i * w + j, h * w + i * w + k, flow_inf);
      }
    }
  }
  i32 ans = dinic (g, src, dst);
  if (ans >= flow_inf) {
    puts("-1");
  } else {
    printf ("%" PRIi32 "\n",ans);
  }
}

int main (void) {
  run ();
  return 0;
}