from collections import deque

h,w,n = list(map(int,input().split()))

factrys = [None] * (n + 1)

ss = ["X" * (w + 2)]

append = ss.append

for i in range(h):

  s = "X" + eval(input()) + "X"

  if "S" in s:

    factrys[0] = (i + 1, s.index("S"))

  for j in range(len(s)):

    if s[j] != 'S' and s[j] != '.' and s[j] != 'X':

      factrys[int(s[j])] = (i + 1, j)

  append(s)

append("X" * (w + 2))



def bfs(i):

  mp = [[-1] * (w + 2) for j in range(h + 2)]

  x,y = factrys[i]

  que = deque()

  append = que.append

  popleft = que.popleft

  append((x, y))

  for count in range(10000000):

    sz = len(que)

    for loop in range(sz):

      (x,y) = popleft()

      if ss[x][y] == 'X' or not mp[x][y] is -1:

        continue

      if (x,y) == factrys[i + 1]:

        return count

      mp[x][y] = count

      append((x + 1, y))

      append((x - 1, y))

      append((x, y + 1))

      append((x, y - 1))



ans = 0

for i in range(n):

  ret = bfs(i)

  if ret is None:

    ans += 0

  else:

    ans += (ret)

print(ans)

from collections import deque



def main():

  h, w, n = list(map(int, input().split()))

  mp = ["X" + eval(input()) + "X" for _ in range(h)]

  mp.insert(0, "X" * (w + 2))

  mp.append("X" * (w + 2))

  

  poss = [None] * (n + 1)

  for y in range(1, h + 1):

    for x in range(1, w + 1):

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

        poss[0] = (x, y)

      elif "1" <= mp[y][x] <= "9":

        poss[int(mp[y][x])] = (x, y)

  

  ans = 0

  vec = ((1, 0), (0, -1), (-1, 0), (0, 1))

  for i in range(n):

    sx, sy = poss[i]

    gx, gy = poss[i + 1]

    used = [[False] * (w + 2) for _ in range(h + 2)]

    used[sy][sx] = True

    que = deque()

    push = que.append

    pop = que.popleft

    push((0, sx, sy))

    while que:

      cnt, x, y = pop()

      if (x, y) == (gx, gy):

        ans += cnt

        break

      

      for dx, dy in vec:

        nx, ny = x + dx, y + dy

        if used[ny][nx] or mp[ny][nx] == "X":continue

        used[ny][nx] = True

        push((cnt + 1, nx, ny))

  

  print(ans)



main()

from collections import deque

h, w, n = list(map(int, input().split()))
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
append = ss.append
for i in range(h):
    s = "X" + eval(input()) + "X"
    if "S" in s:
        factrys[0] = (i + 1, s.index("S"))
    for j in range(len(s)):
        if s[j] != "S" and s[j] != "." and s[j] != "X":
            factrys[int(s[j])] = (i + 1, j)
    append(s)
append("X" * (w + 2))


def bfs(i):
    mp = [[-1] * (w + 2) for j in range(h + 2)]
    x, y = factrys[i]
    que = deque()
    append = que.append
    popleft = que.popleft
    append((x, y))
    for count in range(10000000):
        sz = len(que)
        for loop in range(sz):
            (x, y) = popleft()
            if ss[x][y] == "X" or not mp[x][y] is -1:
                continue
            if (x, y) == factrys[i + 1]:
                return count
            mp[x][y] = count
            append((x + 1, y))
            append((x - 1, y))
            append((x, y + 1))
            append((x, y - 1))


ans = 0
for i in range(n):
    ret = bfs(i)
    if ret is None:
        ans += 0
    else:
        ans += ret
print(ans)

from collections import deque


def main():
    h, w, n = list(map(int, input().split()))
    mp = ["X" + eval(input()) + "X" for _ in range(h)]
    mp.insert(0, "X" * (w + 2))
    mp.append("X" * (w + 2))
    poss = [None] * (n + 1)
    for y in range(1, h + 1):
        for x in range(1, w + 1):
            if mp[y][x] == "S":
                poss[0] = (x, y)
            elif "1" <= mp[y][x] <= "9":
                poss[int(mp[y][x])] = (x, y)
    ans = 0
    vec = ((1, 0), (0, -1), (-1, 0), (0, 1))
    for i in range(n):
        sx, sy = poss[i]
        gx, gy = poss[i + 1]
        used = [[False] * (w + 2) for _ in range(h + 2)]
        used[sy][sx] = True
        que = deque()
        push = que.append
        pop = que.popleft
        push((0, sx, sy))
        while que:
            cnt, x, y = pop()
            if (x, y) == (gx, gy):
                ans += cnt
                break
            for dx, dy in vec:
                nx, ny = x + dx, y + dy
                if used[ny][nx] or mp[ny][nx] == "X":
                    continue
                used[ny][nx] = True
                push((cnt + 1, nx, ny))
    print(ans)


main()

-h, w, n = list(map(int, input().split())) -factrys = [None] * (n + 1) -ss = ["X" * (w + 2)] -append = ss.append -for i in range(h): -    s = "X" + eval(input()) + "X" -    if "S" in s: -        factrys[0] = (i + 1, s.index("S")) -    for j in range(len(s)): -        if s[j] != "S" and s[j] != "." and s[j] != "X": -            factrys[int(s[j])] = (i + 1, j) -    append(s) -append("X" * (w + 2)) + +def main(): +    h, w, n = list(map(int, input().split())) +    mp = ["X" + eval(input()) + "X" for _ in range(h)] +    mp.insert(0, "X" * (w + 2)) +    mp.append("X" * (w + 2)) +    poss = [None] * (n + 1) +    for y in range(1, h + 1): +        for x in range(1, w + 1): +            if mp[y][x] == "S": +                poss[0] = (x, y) +            elif "1" <= mp[y][x] <= "9": +                poss[int(mp[y][x])] = (x, y) +    ans = 0 +    vec = ((1, 0), (0, -1), (-1, 0), (0, 1)) +    for i in range(n): +        sx, sy = poss[i] +        gx, gy = poss[i + 1] +        used = [[False] * (w + 2) for _ in range(h + 2)] +        used[sy][sx] = True +        que = deque() +        push = que.append +        pop = que.popleft +        push((0, sx, sy)) +        while que: +            cnt, x, y = pop() +            if (x, y) == (gx, gy): +                ans += cnt +                break +            for dx, dy in vec: +                nx, ny = x + dx, y + dy +                if used[ny][nx] or mp[ny][nx] == "X": +                    continue +                used[ny][nx] = True +                push((cnt + 1, nx, ny)) +    print(ans) -def bfs(i): -    mp = [[-1] * (w + 2) for j in range(h + 2)] -    x, y = factrys[i] -    que = deque() -    append = que.append -    popleft = que.popleft -    append((x, y)) -    for count in range(10000000): -        sz = len(que) -        for loop in range(sz): -            (x, y) = popleft() -            if ss[x][y] == "X" or not mp[x][y] is -1: -                continue -            if (x, y) == factrys[i + 1]: -                return count -            mp[x][y] = count -            append((x + 1, y)) -            append((x - 1, y)) -            append((x, y + 1)) -            append((x, y - 1)) - - -ans = 0 -for i in range(n): -    ret = bfs(i) -    if ret is None: -        ans += 0 -    else: -        ans += ret -print(ans) +main()