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("....");
WillReturn.Add("#.#.");
WillReturn.Add(".S..");
WillReturn.Add(".##.");
//Yes
}
else if (InputPattern == "Input2") {
WillReturn.Add("2 2");
WillReturn.Add("S.");
WillReturn.Add(".#");
//No
}
else if (InputPattern == "Input3") {
WillReturn.Add("5 7");
WillReturn.Add(".#...#.");
WillReturn.Add("..#.#..");
WillReturn.Add("...S...");
WillReturn.Add("..#.#..");
WillReturn.Add(".#...#.");
//No
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static char[,] mBanArr;
static int UB_X;
static int UB_Y;
static int StaX;
static int StaY;
static void Main()
{
List<string> InputList = GetInputList();
mBanArr = CreateBanArr(InputList.Skip(1));
UB_X = mBanArr.GetUpperBound(0);
UB_Y = mBanArr.GetUpperBound(1);
for (int X = 0; X <= UB_X; X++) {
for (int Y = 0; Y <= UB_Y; Y++) {
if (mBanArr[X, Y] == 'S') {
StaX = X;
StaY = Y;
}
}
}
if (ExecBFS(0, -1)) {
Console.WriteLine("Yes"); return;
}
if (ExecBFS(0, +1)) {
Console.WriteLine("Yes"); return;
}
if (ExecBFS(-1, 0)) {
Console.WriteLine("Yes"); return;
}
if (ExecBFS(+1, 0)) {
Console.WriteLine("Yes"); return;
}
Console.WriteLine("No");
}
static bool ExecBFS(int pVectX, int pVectY)
{
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.Curr_X = StaX;
WillEnqueue.Curr_Y = StaY;
WillEnqueue.Level = 0;
Que.Enqueue(WillEnqueue);
var VisitedSet = new HashSet<long>();
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
// クリア判定
if (Dequeued.Curr_X == StaX && Dequeued.Curr_Y == StaY) {
if (Dequeued.Level == 2) continue;
if (Dequeued.Level >= 4) {
return true;
}
}
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;
long Hash = GetHash(pNewX, pNewY);
bool CanEnqueue = false;
// ゴールは再訪問OK
if (pNewX == StaX && pNewY == StaY) {
CanEnqueue = true;
}
else if (VisitedSet.Add(Hash)) {
CanEnqueue = true;
}
if (CanEnqueue) {
WillEnqueue.Curr_X = pNewX;
WillEnqueue.Curr_Y = pNewY;
WillEnqueue.Level = Dequeued.Level + 1;
Que.Enqueue(WillEnqueue);
}
};
if (Dequeued.Level == 0) {
EnqueueAct(Dequeued.Curr_X + pVectX, Dequeued.Curr_Y + pVectY);
}
else {
EnqueueAct(Dequeued.Curr_X, Dequeued.Curr_Y - 1);
EnqueueAct(Dequeued.Curr_X, Dequeued.Curr_Y + 1);
EnqueueAct(Dequeued.Curr_X - 1, Dequeued.Curr_Y);
EnqueueAct(Dequeued.Curr_X + 1, Dequeued.Curr_Y);
}
}
return false;
}
struct JyoutaiDef
{
internal int Curr_X;
internal int Curr_Y;
internal int Level;
}
static long GetHash(int pX, int pY)
{
return (long)pX * 10000000 + pY;
}
////////////////////////////////////////////////////////////////
// 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;
}
}