

def getStart(field):

    search_field = field[1:]  

    for y in range(len(search_field)):

        for x in range(len(search_field[0])):

            if search_field[y][x] == "S":

                return x,y+1





def bfs(field,start_x,start_y,goal_N):

    direction = [[-1,0],[1,0],[0,-1],[0,1]]

    gotten_cheese = 1

    distance = 0

    que = []

    que.append([start_x,start_y,gotten_cheese,distance])

    INF = 1

    min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]

    while len(que) != 0:

        current = que.pop(0)

        for d in direction:

            nx = current[0] + d[0]

            ny = current[1] + d[1]

            if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X" and min_path[ny-1][nx] == INF:

                if field[ny][nx] == current[2]: 

                    if field[ny][nx] == goal_N: 

                        print((current[3]+1))

                        return

                    else: # not goal, but could eat cheese

                        que = []

                        que.append([nx,ny,current[2]+1,current[3]+1])

                        min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]

                        break

                else:

                    que.append([nx,ny,current[2],current[3]+1])

                    min_path[ny-1][nx] = 0





def main(field):



    sx,sy = getStart(field)

    bfs(field,sx,sy,field[0][2])



matrix = []

while True:

    row = input().rstrip().split()

    if len(row) == 3:

        re_row = []

        for i in row:

            re_row.append(int(i))

        matrix.append(re_row)

    else:

        re_row = []

        for char in row[0]:

            if char.isdigit():

                re_row.append(int(char))

            else:

                re_row.append(char)

        matrix.append(re_row)

    if len(matrix) == int(matrix[0][0]) + 1:

        main(matrix)

        break
import sys

sys.setrecursionlimit(100000000)

 

moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]

 

 

def search(dataset):

    min_time = 0

    mouse = 1

    gx, gy = get_start(dataset)

    for i in range(N):

        gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse)

        mouse += 1

        min_time += min_time_tmp

    print(min_time)

 

 

def bfs(dataset, start, mouse):

    INF = 10000000

    min_time = [[INF for j in range(W)] for i in range(H)]

    min_time[start[0]][start[1]] = 0

    queue = [start]

 

    while len(queue) != 0:

        dx, dy = queue.pop(0)

        for i, j in moves:

            nx = dx + i

            ny = dy + j

            if 0 <= nx < H and 0 <= ny < W and dataset[nx][ny] != 'X' and min_time[nx][ny] == INF:

                cell = dataset[nx][ny]

            else:

                continue

            if type(cell) is int:

                if mouse == int(cell):

                    min_time[nx][ny] = min_time[dx][dy] + 1

                    return nx, ny, min_time[nx][ny]

            min_time[nx][ny] = min_time[dx][dy] + 1

            queue.append((nx, ny))

 

 

def get_start(dataset):

    for i in range(H):

        rows = dataset[i]

        for j in range(W):

            cell = rows[j]

            if cell == 'S':

                return i, j

 

 

dataset = []

while True:

    line = input().rstrip().split()

    if len(line) == 3:

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

    else:

        row = []

        for e in line[0]:

            if e.isdigit():

                row.append(int(e))

            else:

                row.append(e)

        dataset.append(row)

    if len(dataset) == H:

        search(dataset)

        break
def getStart(field):
    search_field = field[1:]
    for y in range(len(search_field)):
        for x in range(len(search_field[0])):
            if search_field[y][x] == "S":
                return x, y + 1


def bfs(field, start_x, start_y, goal_N):
    direction = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    gotten_cheese = 1
    distance = 0
    que = []
    que.append([start_x, start_y, gotten_cheese, distance])
    INF = 1
    min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])]
    while len(que) != 0:
        current = que.pop(0)
        for d in direction:
            nx = current[0] + d[0]
            ny = current[1] + d[1]
            if (
                0 <= nx < field[0][1]
                and 1 <= ny < field[0][0] + 1
                and field[ny][nx] != "X"
                and min_path[ny - 1][nx] == INF
            ):
                if field[ny][nx] == current[2]:
                    if field[ny][nx] == goal_N:
                        print((current[3] + 1))
                        return
                    else:  # not goal, but could eat cheese
                        que = []
                        que.append([nx, ny, current[2] + 1, current[3] + 1])
                        min_path = [
                            [INF for j in range(field[0][1])]
                            for i in range(field[0][0])
                        ]
                        break
                else:
                    que.append([nx, ny, current[2], current[3] + 1])
                    min_path[ny - 1][nx] = 0


def main(field):
    sx, sy = getStart(field)
    bfs(field, sx, sy, field[0][2])


matrix = []
while True:
    row = input().rstrip().split()
    if len(row) == 3:
        re_row = []
        for i in row:
            re_row.append(int(i))
        matrix.append(re_row)
    else:
        re_row = []
        for char in row[0]:
            if char.isdigit():
                re_row.append(int(char))
            else:
                re_row.append(char)
        matrix.append(re_row)
    if len(matrix) == int(matrix[0][0]) + 1:
        main(matrix)
        break

import sys

sys.setrecursionlimit(100000000)
moves = [(0, -1), (0, 1), (-1, 0), (1, 0)]


def search(dataset):
    min_time = 0
    mouse = 1
    gx, gy = get_start(dataset)
    for i in range(N):
        gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse)
        mouse += 1
        min_time += min_time_tmp
    print(min_time)


def bfs(dataset, start, mouse):
    INF = 10000000
    min_time = [[INF for j in range(W)] for i in range(H)]
    min_time[start[0]][start[1]] = 0
    queue = [start]
    while len(queue) != 0:
        dx, dy = queue.pop(0)
        for i, j in moves:
            nx = dx + i
            ny = dy + j
            if (
                0 <= nx < H
                and 0 <= ny < W
                and dataset[nx][ny] != "X"
                and min_time[nx][ny] == INF
            ):
                cell = dataset[nx][ny]
            else:
                continue
            if type(cell) is int:
                if mouse == int(cell):
                    min_time[nx][ny] = min_time[dx][dy] + 1
                    return nx, ny, min_time[nx][ny]
            min_time[nx][ny] = min_time[dx][dy] + 1
            queue.append((nx, ny))


def get_start(dataset):
    for i in range(H):
        rows = dataset[i]
        for j in range(W):
            cell = rows[j]
            if cell == "S":
                return i, j


dataset = []
while True:
    line = input().rstrip().split()
    if len(line) == 3:
        H, W, N = list(map(int, line))
    else:
        row = []
        for e in line[0]:
            if e.isdigit():
                row.append(int(e))
            else:
                row.append(e)
        dataset.append(row)
    if len(dataset) == H:
        search(dataset)
        break

-def getStart(field): -    search_field = field[1:] -    for y in range(len(search_field)): -        for x in range(len(search_field[0])): -            if search_field[y][x] == "S": -                return x, y + 1 +import sys + +sys.setrecursionlimit(100000000) +moves = [(0, -1), (0, 1), (-1, 0), (1, 0)] -def bfs(field, start_x, start_y, goal_N): -    direction = [[-1, 0], [1, 0], [0, -1], [0, 1]] -    gotten_cheese = 1 -    distance = 0 -    que = [] -    que.append([start_x, start_y, gotten_cheese, distance]) -    INF = 1 -    min_path = [[INF for j in range(field[0][1])] for i in range(field[0][0])] -    while len(que) != 0: -        current = que.pop(0) -        for d in direction: -            nx = current[0] + d[0] -            ny = current[1] + d[1] -            if ( -                0 <= nx < field[0][1] -                and 1 <= ny < field[0][0] + 1 -                and field[ny][nx] != "X" -                and min_path[ny - 1][nx] == INF -            ): -                if field[ny][nx] == current[2]: -                    if field[ny][nx] == goal_N: -                        print((current[3] + 1)) -                        return -                    else:  # not goal, but could eat cheese -                        que = [] -                        que.append([nx, ny, current[2] + 1, current[3] + 1]) -                        min_path = [ -                            [INF for j in range(field[0][1])] -                            for i in range(field[0][0]) -                        ] -                        break -                else: -                    que.append([nx, ny, current[2], current[3] + 1]) -                    min_path[ny - 1][nx] = 0 +def search(dataset): +    min_time = 0 +    mouse = 1 +    gx, gy = get_start(dataset) +    for i in range(N): +        gx, gy, min_time_tmp = bfs(dataset, (gx, gy), mouse) +        mouse += 1 +        min_time += min_time_tmp +    print(min_time) -def main(field): -    sx, sy = getStart(field) -    bfs(field, sx, sy, field[0][2]) +def bfs(dataset, start, mouse): +    INF = 10000000 +    min_time = [[INF for j in range(W)] for i in range(H)] +    min_time[start[0]][start[1]] = 0 +    queue = [start] +    while len(queue) != 0: +        dx, dy = queue.pop(0) +        for i, j in moves: +            nx = dx + i +            ny = dy + j +            if ( +                0 <= nx < H +                and 0 <= ny < W +                and dataset[nx][ny] != "X" +                and min_time[nx][ny] == INF +            ): +                cell = dataset[nx][ny] +            else: +                continue +            if type(cell) is int: +                if mouse == int(cell): +                    min_time[nx][ny] = min_time[dx][dy] + 1 +                    return nx, ny, min_time[nx][ny] +            min_time[nx][ny] = min_time[dx][dy] + 1 +            queue.append((nx, ny)) -matrix = [] +def get_start(dataset): +    for i in range(H): +        rows = dataset[i] +        for j in range(W): +            cell = rows[j] +            if cell == "S": +                return i, j + + +dataset = [] -    row = input().rstrip().split() -    if len(row) == 3: -        re_row = [] -        for i in row: -            re_row.append(int(i)) -        matrix.append(re_row) +    line = input().rstrip().split() +    if len(line) == 3: +        H, W, N = list(map(int, line)) -        re_row = [] -        for char in row[0]: -            if char.isdigit(): -                re_row.append(int(char)) +        row = [] +        for e in line[0]: +            if e.isdigit(): +                row.append(int(e)) -                re_row.append(char) -        matrix.append(re_row) -    if len(matrix) == int(matrix[0][0]) + 1: -        main(matrix) +                row.append(e) +        dataset.append(row) +    if len(dataset) == H: +        search(dataset)