from collections import deque

H,W,N=list(map(int,input().split()))

e='X'*(W+2)

M=e+''.join('X'+eval(input())+'X'for _ in[0]*H)+e

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

def f(s,g):

 p,t=s,0

 q=deque()

 b=[1]*len(M);b[s]=0

 while 1:

  for d in m:

   n=p+d

   if'X'!=M[n]and b[n]:

    if M[n]==g:return(n,t+1)

    b[n]=0;q.append([n,t+1])

  p,t=q.popleft()

s=M.find('S')

a=0

for i in range(1,N+1):

 s,t=f(s,str(i))

 a+=t

print(a)

def v():

 from collections import deque

 H,W,N=list(map(int,input().split()))

 e='X'*(W+2)

 M=e+''.join('X'+eval(input())+'X'for _ in[0]*H)+e

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

 def f(s,g):

  p,t=s,0

  q=deque()

  b=[1]*len(M);b[s]=0

  while 1:

   for d in m:

    n=p+d

    if'X'!=M[n]and b[n]:

     if M[n]==g:return(n,t+1)

     b[n]=0;q.append([n,t+1])

   p,t=q.popleft()

 s=M.find('S')

 a=0

 for i in range(1,N+1):

  s,t=f(s,str(i))

  a+=t

 print(a)

if'__main__'==__name__:v()

from collections import deque

H, W, N = list(map(int, input().split()))
e = "X" * (W + 2)
M = e + "".join("X" + eval(input()) + "X" for _ in [0] * H) + e
m = (1, -1, W + 2, -W - 2)


def f(s, g):
    p, t = s, 0
    q = deque()
    b = [1] * len(M)
    b[s] = 0
    while 1:
        for d in m:
            n = p + d
            if "X" != M[n] and b[n]:
                if M[n] == g:
                    return (n, t + 1)
                b[n] = 0
                q.append([n, t + 1])
        p, t = q.popleft()


s = M.find("S")
a = 0
for i in range(1, N + 1):
    s, t = f(s, str(i))
    a += t
print(a)

def v():
    from collections import deque

    H, W, N = list(map(int, input().split()))
    e = "X" * (W + 2)
    M = e + "".join("X" + eval(input()) + "X" for _ in [0] * H) + e
    m = (1, -1, W + 2, -W - 2)

    def f(s, g):
        p, t = s, 0
        q = deque()
        b = [1] * len(M)
        b[s] = 0
        while 1:
            for d in m:
                n = p + d
                if "X" != M[n] and b[n]:
                    if M[n] == g:
                        return (n, t + 1)
                    b[n] = 0
                    q.append([n, t + 1])
            p, t = q.popleft()

    s = M.find("S")
    a = 0
    for i in range(1, N + 1):
        s, t = f(s, str(i))
        a += t
    print(a)


if "__main__" == __name__:
    v()

-from collections import deque +def v(): +    from collections import deque -H, W, N = list(map(int, input().split())) -e = "X" * (W + 2) -M = e + "".join("X" + eval(input()) + "X" for _ in [0] * H) + e -m = (1, -1, W + 2, -W - 2) +    H, W, N = list(map(int, input().split())) +    e = "X" * (W + 2) +    M = e + "".join("X" + eval(input()) + "X" for _ in [0] * H) + e +    m = (1, -1, W + 2, -W - 2) + +    def f(s, g): +        p, t = s, 0 +        q = deque() +        b = [1] * len(M) +        b[s] = 0 +        while 1: +            for d in m: +                n = p + d +                if "X" != M[n] and b[n]: +                    if M[n] == g: +                        return (n, t + 1) +                    b[n] = 0 +                    q.append([n, t + 1]) +            p, t = q.popleft() + +    s = M.find("S") +    a = 0 +    for i in range(1, N + 1): +        s, t = f(s, str(i)) +        a += t +    print(a) -def f(s, g): -    p, t = s, 0 -    q = deque() -    b = [1] * len(M) -    b[s] = 0 -    while 1: -        for d in m: -            n = p + d -            if "X" != M[n] and b[n]: -                if M[n] == g: -                    return (n, t + 1) -                b[n] = 0 -                q.append([n, t + 1]) -        p, t = q.popleft() - - -s = M.find("S") -a = 0 -for i in range(1, N + 1): -    s, t = f(s, str(i)) -    a += t -print(a) +if "__main__" == __name__: +    v()