import sys

file_input = sys.stdin

H, W, N = map(int, file_input.readline().split())

town_map = file_input.read()

start_index = town_map.index('S')

mouse = [start_index, 0, [start_index]]
# position, elapsed time, visited lot

from collections import deque

for goal in range(1, N + 1):
    q = deque()
    goal = str(goal)
    while True:
        cur_pos = mouse[0]
        
        ew_pos = cur_pos % (W + 1)
        if ew_pos < (W - 1):
            next_pos = cur_pos + 1
            lot = town_map[next_pos]
            if lot != 'X' and next_pos not in mouse[2]:
                if lot == goal:
                    mouse = [next_pos, mouse[1] + 1, [next_pos]]
                    break
                else:
                    m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
                    q.append(m)
        if ew_pos > 0:
            next_pos = cur_pos - 1
            lot = town_map[next_pos]
            if lot != 'X' and next_pos not in mouse[2]:
                if lot == goal:
                    mouse = [next_pos, mouse[1] + 1, [next_pos]]
                    break
                else:
                    m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
                    q.append(m)
        
        ns_pos = cur_pos // (W + 1)
        if ns_pos < (H - 1):
            next_pos = cur_pos + W + 1
            lot = town_map[next_pos]
            if lot != 'X' and next_pos not in mouse[2]:
                if lot == goal:
                    mouse = [next_pos, mouse[1] + 1, [next_pos]]
                    break
                else:
                    m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
                    q.append(m)
        if ns_pos > 0:
            next_pos = cur_pos - W - 1
            lot = town_map[next_pos]
            if lot != 'X' and next_pos not in mouse[2]:
                if lot == goal:
                    mouse = [next_pos, mouse[1] + 1, [next_pos]]
                    break
                else:
                    m = [next_pos, mouse[1] + 1, mouse[2] + [next_pos]]
                    q.append(m)
        mouse = q.popleft()

print(mouse[1])


### FIXED CODE ###

import sys

file_input = sys.stdin

H, W, N = map(int, file_input.readline().split())

town_map = 'X' * (W + 2)
for line in file_input:
    town_map += 'X' + line.rstrip() + 'X'
town_map += 'X' * (W + 2)

move = (1, -1, W + 2, -W - 2)

from collections import deque

def search(start_pos, goal_lot):
    mouse = [start_pos, 0] # [position, elapsed time]
    q = deque()
    unvisited = [True] * len(town_map)
    unvisited[start_pos] = False
    while True:
        cur_pos = mouse[0]
        for d in move:
            next_pos = cur_pos + d
            lot = town_map[next_pos]
            if lot != 'X' and unvisited[next_pos]:
                if lot == goal_lot:
                    return (next_pos, mouse[1] + 1)
                else:
                    m = [next_pos, mouse[1] + 1]
                    unvisited[next_pos] = False
                    q.append(m)
        mouse = q.popleft()

def solve():
    start_pos = town_map.find('S')
    time = 0
    for i in range(1, N + 1):
        start_pos, t = search(start_pos, str(i))
        time += t
    print(time)

solve()