AtCoder Regular Contest 074
F
Lotus Leaves
AtCoder
⌀ 2208
原题
← E→
翻译中…

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.

翻译中…

样例

Input #1
3 3
S.o
.o.
o.T
Output #1
2
Remove the upper-right and lower-left leaves.
Input #2
3 4
S...
.oo.
...T
Output #2
0
Input #3
4 3
.S.
.o.
.o.
.T.
Output #3
-1
Input #4
10 10
.o...o..o.
....o.....
....oo.oo.
..oooo..o.
....oo....
..o..o....
o..o....So
o....T....
....o.....
........oo
Output #4
5
题目信息
题号
arc074_d
时间限制
2 s
内存限制
256 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。