import sys
sys.setrecursionlimit(100000000)


def getStart(field):     # before this, cut the first [H,W,N]
    search_field = field[1:]
    for i in range(len(search_field)):
        for j in range(len(search_field[0])):
            if search_field[i][j] == "S":
                print("start point ({},{})".format(i+1,j))
                return i+1,j

def bfs(field,start_x,start_y,goal_N):
    x_direction = [-1,0,1,0]
    y_direction = [0,-1,0,1]
    gotten_cheese = 1
    distance = 0
    que = []
    que.append([start_x,start_y,gotten_cheese,distance])
    while len(que) != 0:
        current = que.pop(0)
        for xd,yd in zip(x_direction,y_direction):
            nx = current[0] + xd
            ny = current[1] + yd
            if 0 <= nx < field[0][1] and 1 <= ny < field[0][0]+1 and field[ny][nx] != "X":
                if field[ny][nx] == current[2]:
                    if field[ny][nx] == goal_N:
                        print("GOAL",current[3]+1)
                        return
                    else:
                        que.append([nx,ny,current[2]+1,current[3]+1])
                else:
                    que.append([nx,ny,current[2],current[3]+1])

def getField():
    matrix = []
    while True:
        row = input().split()
        if len(row) == 1:
            re_row = []
            for char in row[0]:
                if char.isdigit():
                    re_row.append(int(char))
                else:
                    re_row.append(char)
            matrix.append(re_row)
        else:
            matrix.append(row)
        if len(matrix) == int(matrix[0][0]) + 1:
            break
    return matrix

def main():
    # field = [[4,5,2],
    # [".","X",".",".",1],
    # [".",".",".",".","X"],
    # [".","X","X",".","S"],
    # [".",2,".","X","."]]

    # field = [[10,10,9],
    # [".","X",".",".",".","X",".","S",".","X"],
    # [6,".",".",5,"X",".",".","X",1,"X"],
    # [".",".",".","X","X","X","X",".",".","X"],
    # ["X",".",".",9,"X",".",".",".","X","."],
    # [8,".","X",2,"X",".",".","X",3,"X"],
    # [".",".",".","X","X",".","X",4,".","."],
    # ["X","X",".",".",".",".",7,"X",".","."],
    # ["X",".",".","X",".",".","X","X",".","."],
    # ["X",".",".",".","X",".","X","X",".","."],
    # [".",".","X",".",".",".",".",".",".","."]]

    field = getField()

    sx,xy = getStart(field)
    bfs(field,sx,xy,field[0][2])

main()

### FIXED CODE ###

def bfs(field,H,W,start_x,start_y,tmp_N):
    direction = [[-1,0],[1,0],[0,-1],[0,1]]
    que = []
    que.append([start_x,start_y])
    INF = 1000000
    min_path = [[INF] * W for i in range(H)]
    min_path[start_y][start_x] = 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 < W and 0 <= ny < H and field[ny][nx] != "X" and min_path[ny][nx] == INF:
                min_path[ny][nx] = min_path[current[1]][current[0]] + 1
                if field[ny][nx] == tmp_N:
                    return nx,ny,min_path[ny][nx]
                else:
                    que.append([nx,ny])

def getField():
    matrix = []
    first = input().strip()
    H,W,N = map(int,first.split())
    for i in range(H):
        row = list(input().strip())
        for j in range(W):
            if row[j].isdigit():
                row[j] = int(row[j])
        matrix.append(row)
    return matrix,H,W,N

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

def main():
    matrix,H,W,N = getField()
    sx,sy = getStart(matrix)
    distance = 0
    tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,sx,sy,1)
    distance += tmp_dist
    tmps = [tmp_x,tmp_y]
    for k in range(N-1):
        tmp_x,tmp_y,tmp_dist = bfs(matrix,H,W,tmps[0],tmps[1],k+2)
        distance += tmp_dist
        tmps = [tmp_x,tmp_y]
    print(distance)

if __name__=='__main__':
    main()