using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("4 4");
WillReturn.Add("S...");
WillReturn.Add("#..#");
WillReturn.Add("#...");
WillReturn.Add("..#T");
WillReturn.Add("4");
WillReturn.Add("1 1 3");
WillReturn.Add("1 3 5");
WillReturn.Add("3 2 1");
WillReturn.Add("2 3 1");
//Yes
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 2");
WillReturn.Add("S.");
WillReturn.Add("T.");
WillReturn.Add("1");
WillReturn.Add("1 2 4");
//No
}
else if (InputPattern == "Input3") {
WillReturn.Add("4 5");
WillReturn.Add("..#..");
WillReturn.Add(".S##.");
WillReturn.Add(".##T.");
WillReturn.Add(".....");
WillReturn.Add("3");
WillReturn.Add("3 1 5");
WillReturn.Add("1 2 3");
WillReturn.Add("2 2 1");
//Yes
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mH;
static int mW;
static int UB_X;
static int UB_Y;
static char[,] mBanArr;
static int mStaX;
static int mStaY;
static int mGoalX;
static int mGoalY;
// 薬の値[座標のハッシュ値]
static Dictionary<int, int> mKusuriDict = new Dictionary<int, int>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mH = wkArr[0];
mW = wkArr[1];
UB_Y = mH - 1;
UB_X = mW - 1;
mBanArr = CreateBanArr(InputList.Skip(1).Take(mH));
foreach (string Eachstr in InputList.Skip(1 + mH + 1)) {
SplitAct(Eachstr);
int CurrY = wkArr[0] - 1;
int CurrX = wkArr[1] - 1;
int CurrHash = GetHash(CurrX, CurrY);
int Val = wkArr[2];
mKusuriDict[CurrHash] = Val;
}
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
if (mBanArr[X, Y] == 'S') {
mStaX = X; mStaY = Y;
}
if (mBanArr[X, Y] == 'T') {
mGoalX = X; mGoalY = Y;
}
}
}
var Que = new Queue<JyouatiDef>();
JyouatiDef WillEnqueue;
WillEnqueue.CurrX = mStaX;
WillEnqueue.CurrY = mStaY;
WillEnqueue.HP = 0;
Que.Enqueue(WillEnqueue);
// 最大HP[座標のハッシュ値]
var EdakiriDict = new Dictionary<int, int>();
while (Que.Count > 0) {
JyouatiDef Dequeued = Que.Dequeue();
int CurrHash = GetHash(Dequeued.CurrX, Dequeued.CurrY);
// クリア判定
if (Dequeued.CurrX == mGoalX && Dequeued.CurrY == mGoalY) {
Console.WriteLine("Yes");
return;
}
if (mKusuriDict.ContainsKey(CurrHash)) {
Dequeued.HP = Math.Max(Dequeued.HP, mKusuriDict[CurrHash]);
}
if (Dequeued.HP == 0) continue;
Action<int, int> EnqueueAct = (pNewX, pNewY) =>
{
if (pNewX < 0 || UB_X < pNewX) return;
if (pNewY < 0 || UB_Y < pNewY) return;
if (mBanArr[pNewX, pNewY] == '#') return;
WillEnqueue.CurrX = pNewX;
WillEnqueue.CurrY = pNewY;
WillEnqueue.HP = Dequeued.HP - 1;
int NewHash = GetHash(pNewX, pNewY);
if (EdakiriDict.ContainsKey(NewHash)) {
if (EdakiriDict[NewHash] >= WillEnqueue.HP) {
return;
}
}
EdakiriDict[NewHash] = WillEnqueue.HP;
Que.Enqueue(WillEnqueue);
};
EnqueueAct(Dequeued.CurrX, Dequeued.CurrY - 1);
EnqueueAct(Dequeued.CurrX, Dequeued.CurrY + 1);
EnqueueAct(Dequeued.CurrX - 1, Dequeued.CurrY);
EnqueueAct(Dequeued.CurrX + 1, Dequeued.CurrY);
}
Console.WriteLine("No");
}
struct JyouatiDef
{
internal int CurrX;
internal int CurrY;
internal int HP;
}
// 座標のハッシュ値を返す
static int GetHash(int pX, int pY)
{
int Hash = pX * 1000;
Hash += pY;
return Hash;
}
////////////////////////////////////////////////////////////////
// IEnumerable<string>をcharの2次元配列に設定する
////////////////////////////////////////////////////////////////
static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
{
var StrList = pStrEnum.ToList();
if (StrList.Count == 0) {
return new char[0, 0];
}
int UB_X = StrList[0].Length - 1;
int UB_Y = StrList.Count - 1;
char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];
for (int Y = 0; Y <= UB_Y; Y++) {
for (int X = 0; X <= UB_X; X++) {
WillReturn[X, Y] = StrList[Y][X];
}
}
return WillReturn;
}
}