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

stage = [eval(input()) for i in range(h)]

starts = [str(i) for i in range(n)]

goals = [str(i+1) for i in range(n)]

starts_y = [0 for i in range(n)]

starts_x = [0 for i in range(n)]

goals_y = [0 for i in range(n)]

goals_x = [0 for i in range(n)]



starts[0] = "S"

for y in range(h):

    for x in range(w):

        if stage[y][x] in starts:

            starts_y[starts.index(stage[y][x])] = y

            starts_x[starts.index(stage[y][x])] = x

        if stage[y][x] in goals:

            goals_y[goals.index(stage[y][x])] = y

            goals_x[goals.index(stage[y][x])] = x



move_y = [1,-1,0,0]

move_x = [0,0,1,-1]

sum = 0



for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):

    bfs_map = [[-1 for j in range(w)] for i in range(h)]

    data_y = [start_y]

    data_x = [start_x]

    bfs_map[start_y][start_x] = 0

    goal = False



    while len(data_y) != 0 and not goal:

        y = data_y.pop(0)

        x = data_x.pop(0)

        goal = False



        for i in range(4):

            y += move_y[i]

            x += move_x[i]



            if y >= 0 and y < h and x >= 0 and x < w:

                if bfs_map[y][x] == -1 and stage[y][x] != "X":

                    bfs_map[y][x] = bfs_map[y-move_y[i]][x-move_x[i]]+1

                    data_y.append(y)

                    data_x.append(x)

                if bfs_map[goal_y][goal_x] != -1:

                    sum += bfs_map[goal_y][goal_x]

                    goal = True

                    break

            y -= move_y[i]

            x -= move_x[i]



print(sum)

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)







h, w, n = list(map(int, input().split()))
stage = [eval(input()) for i in range(h)]
starts = [str(i) for i in range(n)]
goals = [str(i + 1) for i in range(n)]
starts_y = [0 for i in range(n)]
starts_x = [0 for i in range(n)]
goals_y = [0 for i in range(n)]
goals_x = [0 for i in range(n)]
starts[0] = "S"
for y in range(h):
    for x in range(w):
        if stage[y][x] in starts:
            starts_y[starts.index(stage[y][x])] = y
            starts_x[starts.index(stage[y][x])] = x
        if stage[y][x] in goals:
            goals_y[goals.index(stage[y][x])] = y
            goals_x[goals.index(stage[y][x])] = x
move_y = [1, -1, 0, 0]
move_x = [0, 0, 1, -1]
sum = 0
for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x):
    bfs_map = [[-1 for j in range(w)] for i in range(h)]
    data_y = [start_y]
    data_x = [start_x]
    bfs_map[start_y][start_x] = 0
    goal = False
    while len(data_y) != 0 and not goal:
        y = data_y.pop(0)
        x = data_x.pop(0)
        goal = False
        for i in range(4):
            y += move_y[i]
            x += move_x[i]
            if y >= 0 and y < h and x >= 0 and x < w:
                if bfs_map[y][x] == -1 and stage[y][x] != "X":
                    bfs_map[y][x] = bfs_map[y - move_y[i]][x - move_x[i]] + 1
                    data_y.append(y)
                    data_x.append(x)
                if bfs_map[goal_y][goal_x] != -1:
                    sum += bfs_map[goal_y][goal_x]
                    goal = True
                    break
            y -= move_y[i]
            x -= move_x[i]
print(sum)

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 + -stage = [eval(input()) for i in range(h)] -starts = [str(i) for i in range(n)] -goals = [str(i + 1) for i in range(n)] -starts_y = [0 for i in range(n)] -starts_x = [0 for i in range(n)] -goals_y = [0 for i in range(n)] -goals_x = [0 for i in range(n)] -starts[0] = "S" -for y in range(h): -    for x in range(w): -        if stage[y][x] in starts: -            starts_y[starts.index(stage[y][x])] = y -            starts_x[starts.index(stage[y][x])] = x -        if stage[y][x] in goals: -            goals_y[goals.index(stage[y][x])] = y -            goals_x[goals.index(stage[y][x])] = x -move_y = [1, -1, 0, 0] -move_x = [0, 0, 1, -1] -sum = 0 -for start_y, start_x, goal_y, goal_x in zip(starts_y, starts_x, goals_y, goals_x): -    bfs_map = [[-1 for j in range(w)] for i in range(h)] -    data_y = [start_y] -    data_x = [start_x] -    bfs_map[start_y][start_x] = 0 -    goal = False -    while len(data_y) != 0 and not goal: -        y = data_y.pop(0) -        x = data_x.pop(0) -        goal = False +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])) -            y += move_y[i] -            x += move_x[i] -            if y >= 0 and y < h and x >= 0 and x < w: -                if bfs_map[y][x] == -1 and stage[y][x] != "X": -                    bfs_map[y][x] = bfs_map[y - move_y[i]][x - move_x[i]] + 1 -                    data_y.append(y) -                    data_x.append(x) -                if bfs_map[goal_y][goal_x] != -1: -                    sum += bfs_map[goal_y][goal_x] -                    goal = True -                    break -            y -= move_y[i] -            x -= move_x[i] -print(sum) +            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)