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("3 5");
WillReturn.Add(".S#.G");
WillReturn.Add(".....");
WillReturn.Add(".#...");
//7
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 5");
WillReturn.Add("..#.G");
WillReturn.Add(".....");
WillReturn.Add("S#...");
//-1
}
else if (InputPattern == "Input3") {
WillReturn.Add("8 63");
WillReturn.Add("...............................................................");
WillReturn.Add("..S...#............................#####..#####..#####..####G..");
WillReturn.Add("..#...#................................#..#...#......#..#......");
WillReturn.Add("..#####..####...####..####..#..#...#####..#...#..#####..#####..");
WillReturn.Add("..#...#..#..#...#..#..#..#..#..#...#......#...#..#..........#..");
WillReturn.Add("..#...#..#####..####..####..####...#####..#####..#####..#####..");
WillReturn.Add("................#.....#........#...............................");
WillReturn.Add("................#.....#........#...............................");
//148
}
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 int GoalX;
static int GoalY;
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 (mBanArr[X, Y] == 'G') {
GoalX = X; GoalY = Y;
}
}
}
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnqueue;
WillEnqueue.CurrX = StaX;
WillEnqueue.CurrY = StaY;
WillEnqueue.MoveTate = 0;
WillEnqueue.Level = 0;
Que.Enqueue(WillEnqueue);
WillEnqueue.CurrX = StaX;
WillEnqueue.CurrY = StaY;
WillEnqueue.MoveTate = 1;
WillEnqueue.Level = 0;
Que.Enqueue(WillEnqueue);
var VisitedSet = new HashSet<long>();
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
if (Dequeued.CurrX == GoalX && Dequeued.CurrY == GoalY) {
Console.WriteLine(Dequeued.Level);
return;
}
Action<int, int, int> EnqueueAct = (pNewX, pNewY, pMoveTate) =>
{
if (pNewX < 0 || UB_X < pNewX) return;
if (pNewY < 0 || UB_Y < pNewY) return;
if (mBanArr[pNewX, pNewY] == '#') return;
long Hash = GetHash(pNewX, pNewY, pMoveTate);
if (VisitedSet.Add(Hash)) {
WillEnqueue.CurrX = pNewX;
WillEnqueue.CurrY = pNewY;
WillEnqueue.MoveTate = pMoveTate;
WillEnqueue.Level = Dequeued.Level + 1;
Que.Enqueue(WillEnqueue);
}
};
int MoveTate = Dequeued.MoveTate;
if (MoveTate == 1) {
EnqueueAct(Dequeued.CurrX, Dequeued.CurrY - 1, 0);
EnqueueAct(Dequeued.CurrX, Dequeued.CurrY + 1, 0);
}
else {
EnqueueAct(Dequeued.CurrX - 1, Dequeued.CurrY, 1);
EnqueueAct(Dequeued.CurrX + 1, Dequeued.CurrY, 1);
}
}
Console.WriteLine(-1);
}
static long GetHash(int pX, int pY, int pMoveTate)
{
long Hash = 0;
Hash += pX;
Hash *= 10000;
Hash += pY;
Hash *= 10;
Hash += pMoveTate;
return Hash;
}
struct JyoutaiDef
{
internal int CurrX;
internal int CurrY;
internal int MoveTate;
internal int Level;
}
////////////////////////////////////////////////////////////////
// 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;
}
}