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 3 1");
WillReturn.Add("S..");
WillReturn.Add("...");
WillReturn.Add("..1");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("4 5 2");
WillReturn.Add(".X..1");
WillReturn.Add("....X");
WillReturn.Add(".XX.S");
WillReturn.Add(".2.X.");
//12
}
else if (InputPattern == "Input3") {
WillReturn.Add("10 10 9");
WillReturn.Add(".X...X.S.X");
WillReturn.Add("6..5X..X1X");
WillReturn.Add("...XXXX..X");
WillReturn.Add("X..9X...X.");
WillReturn.Add("8.X2X..X3X");
WillReturn.Add("...XX.X4..");
WillReturn.Add("XX....7X..");
WillReturn.Add("X..X..XX..");
WillReturn.Add("X...X.XX..");
WillReturn.Add("..X.......");
//91
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
struct PointDef
{
internal int X;
internal int Y;
}
static char[,] mBanArr;
static int UB_X;
static int UB_Y;
static void Main()
{
List<string> InputList = GetInputList();
mBanArr = CreateBanArr(InputList.Skip(1));
UB_X = mBanArr.GetUpperBound(0);
UB_Y = mBanArr.GetUpperBound(1);
var PointPosDict = new SortedDictionary<char, PointDef>();
for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
char CurrChar = mBanArr[LoopX, LoopY];
if (CurrChar == 'S') {
CurrChar = '0';
}
if ('0' <= CurrChar && CurrChar <= '9') {
PointDef CurrPos;
CurrPos.X = LoopX;
CurrPos.Y = LoopY;
PointPosDict[CurrChar] = CurrPos;
}
}
}
char[] KeysArr = PointPosDict.Keys.ToArray();
int UB = KeysArr.GetUpperBound(0);
long Answer = 0;
for (int I = 0; I <= UB - 1; I++) {
PointDef StaPos = PointPosDict[KeysArr[I]];
PointDef EndPos = PointPosDict[KeysArr[I + 1]];
Answer += ExecBFS(StaPos, EndPos);
}
Console.WriteLine(Answer);
}
struct JyoutaiDef
{
internal int CurrX;
internal int CurrY;
internal int Level;
}
// スタート座標とゴール座標を引数として、BFSで最短距離を求めて返す
static int ExecBFS(PointDef pStaPos, PointDef pGoalPos)
{
var Que = new Queue<JyoutaiDef>();
JyoutaiDef WillEnque;
WillEnque.CurrX = pStaPos.X;
WillEnque.CurrY = pStaPos.Y;
WillEnque.Level = 0;
Que.Enqueue(WillEnque);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(GetHash(pStaPos.X, pStaPos.Y));
while (Que.Count > 0) {
JyoutaiDef Dequeued = Que.Dequeue();
// クリア判定
if (Dequeued.CurrX == pGoalPos.X && Dequeued.CurrY == pGoalPos.Y) {
return Dequeued.Level;
}
Action<int, int> EnqueAct = (pNewX, pNewY) =>
{
if (pNewX < 0 || UB_X < pNewX) return;
if (pNewY < 0 || UB_Y < pNewY) return;
if (mBanArr[pNewX, pNewY] == 'X') return;
int Hash = GetHash(pNewX, pNewY);
if (VisitedSet.Add(Hash)) {
WillEnque.CurrX = pNewX;
WillEnque.CurrY = pNewY;
WillEnque.Level = Dequeued.Level + 1;
Que.Enqueue(WillEnque);
}
};
EnqueAct(Dequeued.CurrX, Dequeued.CurrY - 1);
EnqueAct(Dequeued.CurrX, Dequeued.CurrY + 1);
EnqueAct(Dequeued.CurrX - 1, Dequeued.CurrY);
EnqueAct(Dequeued.CurrX + 1, Dequeued.CurrY);
}
return -1;
}
static int GetHash(int pX, int pY)
{
return 1000 * pX + 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;
}
}