import copy

import queue

def bfs(count, x, y, cheese):

    q = queue.Queue()

    q.put((count,x,y))

    tempVisited = copy.deepcopy(visited)

    tempVisited[x][y] = True

    

    while q.qsize()>0:

        tempc,tempx,tempy = q.get()

        if geo[tempx][tempy] == cheese:

            return tempc,tempx,tempy

        for i in range(4):

            neox = tempx+dx[i]

            neoy = tempy+dy[i]

            if canGoTo(neox,neoy, tempVisited):

                #if (tempc+1,neox,neoy) not in q:

                q.put((tempc+1,neox,neoy))

                tempVisited[neox][neoy]=True

    return (-1,-1,-1)



def canGoTo(x,y,tempVisited):

    if 0<=x<H and 0<=y<W:

        return not tempVisited[x][y]

    return False



dx = [0,1,0,-1]

dy = [-1,0,1,0]



data = input().split(' ')

H,W,N = list(map(int, data))



#maps

geo = [[None for i in range(W)] for j in range(H)]

visited = [[False for i in range(W)] for j in range(H)]



x,y=-1,-1

for i in range(H):

    line = eval(input())

    for j in range(W):

        p = line[j:j+1]

        geo[i][j] = p

        if p == 'X':

            visited[i][j] = True

        elif p == 'S':

            x,y, = i,j

            

count = 0

for n in range(1,N+1):

    count,x,y = bfs(count,x,y,str(n))

print(count)
import copy

from collections import deque

def bfs(count, x, y, cheese):

    q = deque()

    q.append((count,x,y))

    tempVisited = copy.deepcopy(visited)

    tempVisited[x][y] = True

    

    while len(q)>0:

        tempc,tempx,tempy = q.popleft()

        if geo[tempx][tempy] == cheese:

            return tempc,tempx,tempy

        for i in range(4):

            neox = tempx+dx[i]

            neoy = tempy+dy[i]

            if canGoTo(neox,neoy, tempVisited):

                if (tempc+1,neox,neoy) not in q:

                    q.append((tempc+1,neox,neoy))

                    tempVisited[neox][neoy]=True

    return (-1,-1,-1)



def canGoTo(x,y,tempVisited):

    if 0<=x<H and 0<=y<W:

        return not tempVisited[x][y]

    return False



dx = [0,1,0,-1]

dy = [-1,0,1,0]



data = input().split(' ')

H,W,N = list(map(int, data))



#maps

geo = [[None for i in range(W)] for j in range(H)]

visited = [[False for i in range(W)] for j in range(H)]



x,y=-1,-1

for i in range(H):

    line = eval(input())

    for j in range(W):

        p = line[j:j+1]

        geo[i][j] = p

        if p == 'X':

            visited[i][j] = True

        elif p == 'S':

            x,y, = i,j

            

count = 0

for n in range(1,N+1):

    count,x,y = bfs(count,x,y,str(n))

print(count)
import copy
import queue


def bfs(count, x, y, cheese):
    q = queue.Queue()
    q.put((count, x, y))
    tempVisited = copy.deepcopy(visited)
    tempVisited[x][y] = True
    while q.qsize() > 0:
        tempc, tempx, tempy = q.get()
        if geo[tempx][tempy] == cheese:
            return tempc, tempx, tempy
        for i in range(4):
            neox = tempx + dx[i]
            neoy = tempy + dy[i]
            if canGoTo(neox, neoy, tempVisited):
                # if (tempc+1,neox,neoy) not in q:
                q.put((tempc + 1, neox, neoy))
                tempVisited[neox][neoy] = True
    return (-1, -1, -1)


def canGoTo(x, y, tempVisited):
    if 0 <= x < H and 0 <= y < W:
        return not tempVisited[x][y]
    return False


dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
data = input().split(" ")
H, W, N = list(map(int, data))
# maps
geo = [[None for i in range(W)] for j in range(H)]
visited = [[False for i in range(W)] for j in range(H)]
x, y = -1, -1
for i in range(H):
    line = eval(input())
    for j in range(W):
        p = line[j : j + 1]
        geo[i][j] = p
        if p == "X":
            visited[i][j] = True
        elif p == "S":
            x, y, = (
                i,
                j,
            )
count = 0
for n in range(1, N + 1):
    count, x, y = bfs(count, x, y, str(n))
print(count)

import copy
from collections import deque


def bfs(count, x, y, cheese):
    q = deque()
    q.append((count, x, y))
    tempVisited = copy.deepcopy(visited)
    tempVisited[x][y] = True
    while len(q) > 0:
        tempc, tempx, tempy = q.popleft()
        if geo[tempx][tempy] == cheese:
            return tempc, tempx, tempy
        for i in range(4):
            neox = tempx + dx[i]
            neoy = tempy + dy[i]
            if canGoTo(neox, neoy, tempVisited):
                if (tempc + 1, neox, neoy) not in q:
                    q.append((tempc + 1, neox, neoy))
                    tempVisited[neox][neoy] = True
    return (-1, -1, -1)


def canGoTo(x, y, tempVisited):
    if 0 <= x < H and 0 <= y < W:
        return not tempVisited[x][y]
    return False


dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
data = input().split(" ")
H, W, N = list(map(int, data))
# maps
geo = [[None for i in range(W)] for j in range(H)]
visited = [[False for i in range(W)] for j in range(H)]
x, y = -1, -1
for i in range(H):
    line = eval(input())
    for j in range(W):
        p = line[j : j + 1]
        geo[i][j] = p
        if p == "X":
            visited[i][j] = True
        elif p == "S":
            x, y, = (
                i,
                j,
            )
count = 0
for n in range(1, N + 1):
    count, x, y = bfs(count, x, y, str(n))
print(count)

-import queue +from collections import deque -    q = queue.Queue() -    q.put((count, x, y)) +    q = deque() +    q.append((count, x, y)) -    while q.qsize() > 0: -        tempc, tempx, tempy = q.get() +    while len(q) > 0: +        tempc, tempx, tempy = q.popleft() -                # if (tempc+1,neox,neoy) not in q: -                q.put((tempc + 1, neox, neoy)) -                tempVisited[neox][neoy] = True +                if (tempc + 1, neox, neoy) not in q: +                    q.append((tempc + 1, neox, neoy)) +                    tempVisited[neox][neoy] = True