(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
;;;
;;; Ford-Fulkerson
;;;

(setf *print-circle* t)

(defstruct (edge (:constructor %make-edge))
  (to nil :type fixnum)
  (capacity 0 :type fixnum)
  (flow 0 :type fixnum)
  (reversed nil :type (or null edge)))

(declaim (inline push-edge))
(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"
  (let* ((dep (%make-edge :to to-idx :capacity capacity))
         (ret (%make-edge :to 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 ((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-flow edge) (edge-capacity edge)))
          (let ((flow (%find-flow (edge-to edge)
                                  dest-idx
                                  graph
                                  (min max-flow (- (edge-capacity edge) (edge-flow edge)))
                                  checked)))
            (declare ((integer 0 #.most-positive-fixnum) flow))
            (unless (zerop flow)
              (incf (edge-flow edge) flow)
              (incf (edge-capacity (edge-reversed edge)) flow)
              (return flow)))))))

(defun max-flow (src-idx dest-idx graph)
  (declare ((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))))

;; For test
;; (progn
;;   (defparameter *graph* (make-array '(5) :element-type 'edge :initial-element nil))
;;   (add-edge 0 1 10 *graph*)
;;   (add-edge 0 2 2 *graph*)
;;   (add-edge 1 2 6 *graph*)
;;   (add-edge 1 3 6 *graph*)
;;   (add-edge 3 2 3 *graph*)
;;   (add-edge 3 4 8 *graph*)
;;   (add-edge 2 4 5 *graph*)
;;   (assert (= 11 (max-flow 0 4 *graph*))))

(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))))))

(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

(defconstant +inf+ #xffffffff)
(defun main ()
  (declare #.OPT)
  (let* ((h (read))
         (w (read))
         (graph (make-array (+ h w 2) :element-type 'list :initial-element nil))
         (start-index (+ h w))
         (goal-index (+ h w 1))
         (start-i 0)
         (start-j 0)
         (goal-i 0)
         (goal-j 0))
    (declare (uint7 h w))
    (dotimes (i h)
      (let ((line (buffered-read-line 100)))
        (dotimes (j w)
          (case (aref line j)
            (#\o (push-edge i (+ j h) 1 graph :bidirectional t))
            (#\S
             (setf start-i i start-j j)
             (push-edge start-index i +inf+ graph)
             (push-edge start-index (+ j h) +inf+ graph))
            (#\T
             (setf goal-i i goal-j j)
             (push-edge i goal-index +inf+ graph)
             (push-edge (+ j h) goal-index +inf+ graph))))))
    (when (or (= start-i goal-i) (= start-j goal-j))
      (println -1)
      (return-from main))
    (println (max-flow start-index goal-index graph))))

#-swank(main)


Measured memory_bytes: 19048