from collections import deque



h, w, n = list(map(int, input().split()))

g = ['0'] * h



sx, sy = -1, -1

pos = -1

for i in range(h):

    g[i] = eval(input())

    if pos == -1:

        pos = g[i].find('S')

        if pos != -1:

            sx = i

            sy = pos



book = set()

queue = deque()



ans = 0



Next = [[-1, 0], [1, 0], [0, -1], [0, 1]]



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

    book.clear()

    queue.clear()

    queue.append((sx, sy, 0))

    aim = str(a)



    while len(queue):

        t = queue.popleft()

        if (t[0], t[1]) in book:

            continue



        if g[t[0]][t[1]] == aim:

            ans += t[2]

            # print(t[0], ' ', t[1], ' ', aim, ' ', ans)

            sx, sy = t[0], t[1]

            break



        book.add((t[0], t[1]))

        for i in range(4):

            tx = t[0] + Next[i][0]

            ty = t[1] + Next[i][1]

            if 0 <= tx < h and 0 <= ty < w and g[tx][ty] != 'X':

                queue.append((tx, ty, t[2] + 1))



print(ans)







from collections import deque, defaultdict



d = [[1, 0], [-1, 0], [0, 1], [0, -1]]



H, W, N = list(map(int, input().split()))



G = []



sx = sy = 0

for i in range(H):

    G.append(eval(input()))

    for j, c in enumerate(G[i]):

        if c == 'S':

            sx, sy = i, j





Q = deque()

Q.append((sx, sy, 0))

visited = defaultdict(lambda: defaultdict(int))

visited[sx][sy] = 1



target = '1'



while len(Q):

    x, y, step = Q.popleft()

    if G[x][y] == target:

        visited.clear()

        Q.clear()

        target = str(int(target) + 1)

        if target == str(N + 1):

            print(step)

            break

    for dx, dy in d:

        nx, ny = x + dx, y + dy

        if 0 <= nx < H and 0 <= ny < W and G[nx][ny] != 'X':

            if not visited[nx][ny]:

                visited[nx][ny] = 1

                Q.append((nx, ny, step + 1))

from collections import deque

h, w, n = list(map(int, input().split()))
g = ["0"] * h
sx, sy = -1, -1
pos = -1
for i in range(h):
    g[i] = eval(input())
    if pos == -1:
        pos = g[i].find("S")
        if pos != -1:
            sx = i
            sy = pos
book = set()
queue = deque()
ans = 0
Next = [[-1, 0], [1, 0], [0, -1], [0, 1]]
for a in range(1, n + 1):
    book.clear()
    queue.clear()
    queue.append((sx, sy, 0))
    aim = str(a)
    while len(queue):
        t = queue.popleft()
        if (t[0], t[1]) in book:
            continue
        if g[t[0]][t[1]] == aim:
            ans += t[2]
            # print(t[0], ' ', t[1], ' ', aim, ' ', ans)
            sx, sy = t[0], t[1]
            break
        book.add((t[0], t[1]))
        for i in range(4):
            tx = t[0] + Next[i][0]
            ty = t[1] + Next[i][1]
            if 0 <= tx < h and 0 <= ty < w and g[tx][ty] != "X":
                queue.append((tx, ty, t[2] + 1))
print(ans)

from collections import deque, defaultdict

d = [[1, 0], [-1, 0], [0, 1], [0, -1]]
H, W, N = list(map(int, input().split()))
G = []
sx = sy = 0
for i in range(H):
    G.append(eval(input()))
    for j, c in enumerate(G[i]):
        if c == "S":
            sx, sy = i, j
Q = deque()
Q.append((sx, sy, 0))
visited = defaultdict(lambda: defaultdict(int))
visited[sx][sy] = 1
target = "1"
while len(Q):
    x, y, step = Q.popleft()
    if G[x][y] == target:
        visited.clear()
        Q.clear()
        target = str(int(target) + 1)
        if target == str(N + 1):
            print(step)
            break
    for dx, dy in d:
        nx, ny = x + dx, y + dy
        if 0 <= nx < H and 0 <= ny < W and G[nx][ny] != "X":
            if not visited[nx][ny]:
                visited[nx][ny] = 1
                Q.append((nx, ny, step + 1))

-from collections import deque +from collections import deque, defaultdict -h, w, n = list(map(int, input().split())) -g = ["0"] * h -sx, sy = -1, -1 -pos = -1 -for i in range(h): -    g[i] = eval(input()) -    if pos == -1: -        pos = g[i].find("S") -        if pos != -1: -            sx = i -            sy = pos -book = set() -queue = deque() -ans = 0 -Next = [[-1, 0], [1, 0], [0, -1], [0, 1]] -for a in range(1, n + 1): -    book.clear() -    queue.clear() -    queue.append((sx, sy, 0)) -    aim = str(a) -    while len(queue): -        t = queue.popleft() -        if (t[0], t[1]) in book: -            continue -        if g[t[0]][t[1]] == aim: -            ans += t[2] -            # print(t[0], ' ', t[1], ' ', aim, ' ', ans) -            sx, sy = t[0], t[1] +d = [[1, 0], [-1, 0], [0, 1], [0, -1]] +H, W, N = list(map(int, input().split())) +G = [] +sx = sy = 0 +for i in range(H): +    G.append(eval(input())) +    for j, c in enumerate(G[i]): +        if c == "S": +            sx, sy = i, j +Q = deque() +Q.append((sx, sy, 0)) +visited = defaultdict(lambda: defaultdict(int)) +visited[sx][sy] = 1 +target = "1" +while len(Q): +    x, y, step = Q.popleft() +    if G[x][y] == target: +        visited.clear() +        Q.clear() +        target = str(int(target) + 1) +        if target == str(N + 1): +            print(step) -        book.add((t[0], t[1])) -        for i in range(4): -            tx = t[0] + Next[i][0] -            ty = t[1] + Next[i][1] -            if 0 <= tx < h and 0 <= ty < w and g[tx][ty] != "X": -                queue.append((tx, ty, t[2] + 1)) -print(ans) +    for dx, dy in d: +        nx, ny = x + dx, y + dy +        if 0 <= nx < H and 0 <= ny < W and G[nx][ny] != "X": +            if not visited[nx][ny]: +                visited[nx][ny] = 1 +                Q.append((nx, ny, step + 1))