import queue

class unko(Exception):pass

INF=1145141919

dx=[1,-1,0,0]

dy=[0,0,1,-1]

def search(cor_s,dst):

    mem=[[INF for i in range(a[1])]for j in range(a[0])]

    q=queue.Queue()

    q.put(cor_s)

    mem[cor_s[0]][cor_s[1]]=0

    while not q.empty():

        cor=q.get()

        for i in range(4):

            x=cor[0]+dx[i]

            y=cor[1]+dy[i]

            if 0<=x<a[0] and 0<=y<a[1] and s[x][y]!='X' and mem[x][y]==INF:

                mem[x][y]=mem[cor[0]][cor[1]]+1

                if s[x][y]==str(dst):return [[x,y],mem[x][y]]

                else:q.put([x,y])

#

a=[int(num)for num in input().split(' ')]

s=[list(eval(input())) for i in range(a[0])]

pos_s=[]

time=0

for li in s:

    if 'S' in li:pos_s=[s.index(li),li.index('S')]

for i in range(a[2]):

    ret_l=search(pos_s,i+1)

    pos_s=ret_l[0]

    time+=ret_l[1]

print(time)
#import queue #queue

INF=1145141919

dx=[1,-1,0,0]

dy=[0,0,1,-1]

def search(cor_s,dst):

    mem=[[INF for i in range(a[1])]for j in range(a[0])]

    #q=queue.Queue() #queue

    #q.put(cor_s) #queue

    li=[cor_s] #list

    mem[cor_s[0]][cor_s[1]]=0

    #while not q.empty(): #queue

    while len(li)!=0: #list

        #cor=q.get() #queue

        cor=li.pop(0) #list

        for i in range(4):

            x=cor[0]+dx[i]

            y=cor[1]+dy[i]

            if 0<=x<a[0] and 0<=y<a[1] and s[x][y]!='X' and mem[x][y]==INF:

                mem[x][y]=mem[cor[0]][cor[1]]+1

                if s[x][y]==str(dst):return [[x,y],mem[x][y]]

                else:

                    #q.put([x,y]) #queue

                    li.append([x,y])

#

a=[int(num)for num in input().split(' ')]

s=[list(eval(input())) for i in range(a[0])]

pos_s=[]

time=0

for li in s:

    if 'S' in li:pos_s=[s.index(li),li.index('S')]

for i in range(a[2]):

    ret_l=search(pos_s,i+1)

    pos_s=ret_l[0]

    time+=ret_l[1]

print(time)
import queue


class unko(Exception):
    pass


INF = 1145141919
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]


def search(cor_s, dst):
    mem = [[INF for i in range(a[1])] for j in range(a[0])]
    q = queue.Queue()
    q.put(cor_s)
    mem[cor_s[0]][cor_s[1]] = 0
    while not q.empty():
        cor = q.get()
        for i in range(4):
            x = cor[0] + dx[i]
            y = cor[1] + dy[i]
            if 0 <= x < a[0] and 0 <= y < a[1] and s[x][y] != "X" and mem[x][y] == INF:
                mem[x][y] = mem[cor[0]][cor[1]] + 1
                if s[x][y] == str(dst):
                    return [[x, y], mem[x][y]]
                else:
                    q.put([x, y])


#
a = [int(num) for num in input().split(" ")]
s = [list(eval(input())) for i in range(a[0])]
pos_s = []
time = 0
for li in s:
    if "S" in li:
        pos_s = [s.index(li), li.index("S")]
for i in range(a[2]):
    ret_l = search(pos_s, i + 1)
    pos_s = ret_l[0]
    time += ret_l[1]
print(time)

# import queue #queue
INF = 1145141919
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]


def search(cor_s, dst):
    mem = [[INF for i in range(a[1])] for j in range(a[0])]
    # q=queue.Queue() #queue
    # q.put(cor_s) #queue
    li = [cor_s]  # list
    mem[cor_s[0]][cor_s[1]] = 0
    # while not q.empty(): #queue
    while len(li) != 0:  # list
        # cor=q.get() #queue
        cor = li.pop(0)  # list
        for i in range(4):
            x = cor[0] + dx[i]
            y = cor[1] + dy[i]
            if 0 <= x < a[0] and 0 <= y < a[1] and s[x][y] != "X" and mem[x][y] == INF:
                mem[x][y] = mem[cor[0]][cor[1]] + 1
                if s[x][y] == str(dst):
                    return [[x, y], mem[x][y]]
                else:
                    # q.put([x,y]) #queue
                    li.append([x, y])


#
a = [int(num) for num in input().split(" ")]
s = [list(eval(input())) for i in range(a[0])]
pos_s = []
time = 0
for li in s:
    if "S" in li:
        pos_s = [s.index(li), li.index("S")]
for i in range(a[2]):
    ret_l = search(pos_s, i + 1)
    pos_s = ret_l[0]
    time += ret_l[1]
print(time)

-import queue - - -class unko(Exception): -    pass - - +# import queue #queue -    q = queue.Queue() -    q.put(cor_s) +    # q=queue.Queue() #queue +    # q.put(cor_s) #queue +    li = [cor_s]  # list -    while not q.empty(): -        cor = q.get() +    # while not q.empty(): #queue +    while len(li) != 0:  # list +        # cor=q.get() #queue +        cor = li.pop(0)  # list -                    q.put([x, y]) +                    # q.put([x,y]) #queue +                    li.append([x, y])