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 Common Lisp (SBCL 1.1.14) (judge verdict: Time Limit Exceeded)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (terminate-char #\Space))
  "Note that the returned string will be reused."
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let ((,buffer (load-time-value (make-string ,buffer-size
                                                  :element-type 'base-char))))
       (declare (simple-base-string ,buffer))
       (loop for ,character of-type base-char =
                #-swank (code-char (read-byte ,in nil #.(char-code #\Newline)))
                #+swank (read-char ,in nil #\Newline)
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (when (< ,idx ,buffer-size)
                       (setf (schar ,buffer ,idx) ,terminate-char))
                     (return (values ,buffer ,idx))))))

;;;
;;; Ford-Fulkerson
;;;
(deftype edge () '(cons (unsigned-byte 62) t))

(declaim (inline %make-edge))
(defun %make-edge (to &key (capacity 0) reversed)
  (declare ((unsigned-byte 31) to capacity)
           (values edge))
  (cons (+ capacity (ash to 31))
        reversed))

(declaim (inline edge-reversed))
(defun edge-reversed (edge)
  (cdr edge))

(declaim (inline (setf edge-reversed)))
(defun (setf edge-reversed) (new-value edge)
  (setf (cdr edge) new-value))

(declaim (inline edge-capacity))
(defun edge-capacity (edge)
  (logand (car edge) #.(- (expt 2 31) 1)))

(declaim (inline (setf edge-capacity)))
(defun (setf edge-capacity) (new-value edge)
  (declare ((unsigned-byte 31) new-value))
  (setf (car edge)
        (logior new-value (logand (car edge) #.(ash (- (expt 2 31) 1) 31)))))

(declaim (inline edge-to))
(defun edge-to (edge)
  (ash (the (unsigned-byte 62) (car edge)) -31))

(defun push-edge (from-idx to-idx capacity graph &key bidirectional)
  "FROM-IDX, TO-IDX := index of vertex
GRAPH := vector of list of all the edges that goes from the vertex"
  (declare #.OPT
           ((simple-array list (*)) graph))
  (let* ((dep (%make-edge to-idx :capacity capacity))
         (ret (%make-edge from-idx
                          :capacity (if bidirectional capacity 0)
                          :reversed dep)))
    (setf (edge-reversed dep) ret)
    (push dep (aref graph from-idx))
    (push ret (aref graph to-idx))))

(defun %find-flow (src-idx dest-idx graph max-flow checked)
  "DFS"
  (declare #.OPT
           ((integer 0 #.most-positive-fixnum) src-idx dest-idx max-flow)
           (simple-bit-vector checked)
           ((simple-array list (*)) graph))
  (setf (aref checked src-idx) 1)
  (if (= src-idx dest-idx)
      max-flow
      (dolist (edge (aref graph src-idx) 0)
        (when (and (zerop (aref checked (edge-to edge)))
                   (> (edge-capacity edge) 0))
          (let ((flow (%find-flow (edge-to edge)
                                  dest-idx
                                  graph
                                  (min max-flow (edge-capacity edge))
                                  checked)))
            (declare ((integer 0 #.most-positive-fixnum) flow))
            (unless (zerop flow)
              (decf (edge-capacity edge) flow)
              (incf (edge-capacity (edge-reversed edge)) flow)
              (return flow)))))))

(defun max-flow (src-idx dest-idx graph)
  (declare #.OPT ((simple-array list (*)) graph))
  (let ((checked (make-array (length graph) :element-type 'bit :initial-element 0)))
    (loop for incr-flow of-type (integer 0 #.most-positive-fixnum)
             = (%find-flow src-idx dest-idx graph most-positive-fixnum checked)
          do (fill checked 0)
          until (zerop incr-flow)
          sum incr-flow of-type (integer 0 #.most-positive-fixnum))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Hauptteil

(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (plan (make-array (list h w) :element-type 'bit :initial-element 0))
         (graph (make-array (* h w 2) :element-type 'list :initial-element nil))
         (start-i 0)
         (start-j 0)
         (goal-i 0)
         (goal-j 0))
    (declare (uint7 h w start-i start-j goal-i goal-j))
    (macrolet ((trans-coord (i j)
                 `(* 2 (+ (* w ,i) ,j))))
      (dotimes (i h)
        (let ((line (buffered-read-line 100)))
          (dotimes (j w)
            (case (aref line j)
              (#\o (setf (aref plan i j) 1)
               (let ((index (trans-coord i j)))
                 (push-edge index (1+ index) 1 graph)))
              (#\S (setf start-i i
                         start-j j))
              (#\T (setf goal-i i
                         goal-j j))))))
      (when (or (= start-i goal-i) (= start-j goal-j))
        (println -1)
        (return-from main))
      (let ((start-index (trans-coord start-i start-j))
            (goal-index (trans-coord goal-i goal-j)))
        (dotimes (i h)
          (loop for row-rest on (loop for j below w
                                      when (= 1 (aref plan i j))
                                      collect j)
                for init-j of-type uint7 = (car row-rest)
                for init-coord = (trans-coord i init-j)
                do (loop for dest-j of-type uint7 in (cdr row-rest)
                         for dest-coord = (trans-coord i dest-j)
                         do (push-edge (1+ init-coord) dest-coord #xffff graph)
                            (push-edge (1+ dest-coord) init-coord #xffff graph))))
        (dotimes (j w)
          (loop for col-rest on (loop for i below h
                                      when (= 1 (aref plan i j))
                                      collect i)
                for init-i of-type uint7 = (car col-rest)
                for init-coord = (trans-coord init-i j)
                do (loop for dest-i of-type uint7 in (cdr col-rest)
                         for dest-coord = (trans-coord dest-i j)
                         do (push-edge (1+ init-coord) dest-coord #xffff graph)
                            (push-edge (1+ dest-coord) init-coord #xffff graph))))
        (loop for j of-type uint7 in (loop for j below w
                                           when (= 1 (aref plan start-i j))
                                           collect j)
              do (push-edge (1+ start-index) (trans-coord start-i j) #xffff graph))
        (loop for j of-type uint7 in (loop for j below w
                                           when (= 1 (aref plan goal-i j))
                                           collect j)
              do (push-edge (1+ (trans-coord goal-i j)) goal-index #xffff graph))
        (loop for i of-type uint7 in (loop for i below h
                                           when (= 1 (aref plan i start-j))
                                           collect i)
              do (push-edge (1+ start-index) (trans-coord i start-j) #xffff graph))
        (loop for i of-type uint7 in (loop for i below h
                                           when (= 1 (aref plan i goal-j))
                                           collect i)
              do (push-edge (1+ (trans-coord i goal-j)) goal-index #xffff graph))
        (println (max-flow (1+ start-index) goal-index graph))))))

#-swank(main)