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()))

field = [list(eval(input())) for i in range(H)]

node_posi = [0 for i in range(N+1)]



nodes = 'S123456789'

for i in range(H):

	for j in range(W):

		if field[i][j] in nodes:

			node_posi[nodes.index(field[i][j])] = [j, i]



cost = 0

for i in range(len(node_posi)-1):

	searched = [[-1]*W for i in range(H)]

	start, goal = node_posi[i], node_posi[i+1]

	Q = deque([start])

	searched[start[1]][start[0]] = 0

	goal_flag = False

	

	while Q and not goal_flag:

		x, y = Q.popleft()

		goal_flag = False



		for q in range(4):

			nx = x + [1,0,-1,0][q]

			ny = y + [0,1,0,-1][q]

			

			if nx >= 0 and nx < W and ny >= 0 and ny < H:

				if searched[ny][nx] == -1 and field[ny][nx] != 'X':

					Q.append([nx, ny])

					searched[ny][nx] = searched[y][x] + 1

					if ([nx, ny] == goal):

						cost += searched[ny][nx]

						goal_flag = True

						break



print(cost)

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()))
field = [list(eval(input())) for i in range(H)]
node_posi = [0 for i in range(N + 1)]
nodes = "S123456789"
for i in range(H):
    for j in range(W):
        if field[i][j] in nodes:
            node_posi[nodes.index(field[i][j])] = [j, i]
cost = 0
for i in range(len(node_posi) - 1):
    searched = [[-1] * W for i in range(H)]
    start, goal = node_posi[i], node_posi[i + 1]
    Q = deque([start])
    searched[start[1]][start[0]] = 0
    goal_flag = False
    while Q and not goal_flag:
        x, y = Q.popleft()
        goal_flag = False
        for q in range(4):
            nx = x + [1, 0, -1, 0][q]
            ny = y + [0, 1, 0, -1][q]
            if nx >= 0 and nx < W and ny >= 0 and ny < H:
                if searched[ny][nx] == -1 and field[ny][nx] != "X":
                    Q.append([nx, ny])
                    searched[ny][nx] = searched[y][x] + 1
                    if [nx, ny] == goal:
                        cost += searched[ny][nx]
                        goal_flag = True
                        break
print(cost)

-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())) +field = [list(eval(input())) for i in range(H)] +node_posi = [0 for i in range(N + 1)] +nodes = "S123456789" +for i in range(H): +    for j in range(W): +        if field[i][j] in nodes: +            node_posi[nodes.index(field[i][j])] = [j, i] +cost = 0 +for i in range(len(node_posi) - 1): +    searched = [[-1] * W for i in range(H)] +    start, goal = node_posi[i], node_posi[i + 1] +    Q = deque([start]) +    searched[start[1]][start[0]] = 0 +    goal_flag = False +    while Q and not goal_flag: +        x, y = Q.popleft() +        goal_flag = False +        for q in range(4): +            nx = x + [1, 0, -1, 0][q] +            ny = y + [0, 1, 0, -1][q] +            if nx >= 0 and nx < W and ny >= 0 and ny < H: +                if searched[ny][nx] == -1 and field[ny][nx] != "X": +                    Q.append([nx, ny]) +                    searched[ny][nx] = searched[y][x] + 1 +                    if [nx, ny] == goal: +                        cost += searched[ny][nx] +                        goal_flag = True +                        break +print(cost)