import queue

from collections import deque

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

factrys = [None] * (n + 1)

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

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(1,n + 1):

    if str(j) in s:

      factrys[j] = (i + 1, s.index(str(j)))

  ss.append(s)

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



def bfs(i):

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

  x,y = factrys[i]

  mp[x][y] = 0

  que = queue.Queue()

  que.put((x, y))

  while True:

    (x,y) = que.get()

  #  print(i,x,y)

  #  print(mp[x][y])

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

      return mp[x][y]

    count = mp[x][y]

    if ss[x + 1][y] != "X" and mp[x + 1][y] is None:

      mp[x + 1][y] = count + 1

      que.put((x + 1, y))

    if ss[x - 1][y] != "X" and mp[x - 1][y] is None:

      mp[x - 1][y] = count + 1

      que.put((x - 1, y))

    if ss[x][y + 1] != "X" and mp[x][y + 1] is None:

      mp[x][y + 1] = count + 1

      que.put((x, y + 1))

    if ss[x][y - 1] != "X" and mp[x][y - 1] is None:

      mp[x][y - 1] = count + 1

      que.put((x, y - 1))



ans = 0

for i in range(n):

  ret = bfs(i)

  if ret is None:

    ans += 0

  else:

    ans += (ret)

print(ans)

import queue

from collections import deque

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

factrys = [None] * (n + 1)

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

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(1,n + 1):

    if str(j) in s:

      factrys[j] = (i + 1, s.index(str(j)))

  ss.append(s)

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



def bfs(i):

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

  x,y = factrys[i]

  mp[x][y] = 0

  que = deque()

  que.append((x, y))

  while True:

    (x,y) = que.popleft()

  #  print(i,x,y)

  #  print(mp[x][y])

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

      return mp[x][y]

    count = mp[x][y]

    if ss[x + 1][y] != "X" and mp[x + 1][y] is None:

      mp[x + 1][y] = count + 1

      que.append((x + 1, y))

    if ss[x - 1][y] != "X" and mp[x - 1][y] is None:

      mp[x - 1][y] = count + 1

      que.append((x - 1, y))

    if ss[x][y + 1] != "X" and mp[x][y + 1] is None:

      mp[x][y + 1] = count + 1

      que.append((x, y + 1))

    if ss[x][y - 1] != "X" and mp[x][y - 1] is None:

      mp[x][y - 1] = count + 1

      que.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)

import queue
from collections import deque

h, w, n = list(map(int, input().split()))
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
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(1, n + 1):
        if str(j) in s:
            factrys[j] = (i + 1, s.index(str(j)))
    ss.append(s)
ss.append("X" * (w + 2))


def bfs(i):
    mp = [[None] * (w + 2) for j in range(h + 2)]
    x, y = factrys[i]
    mp[x][y] = 0
    que = queue.Queue()
    que.put((x, y))
    while True:
        (x, y) = que.get()
        #  print(i,x,y)
        #  print(mp[x][y])
        if (x, y) == factrys[i + 1]:
            return mp[x][y]
        count = mp[x][y]
        if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
            mp[x + 1][y] = count + 1
            que.put((x + 1, y))
        if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
            mp[x - 1][y] = count + 1
            que.put((x - 1, y))
        if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
            mp[x][y + 1] = count + 1
            que.put((x, y + 1))
        if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
            mp[x][y - 1] = count + 1
            que.put((x, y - 1))


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

import queue
from collections import deque

h, w, n = list(map(int, input().split()))
factrys = [None] * (n + 1)
ss = ["X" * (w + 2)]
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(1, n + 1):
        if str(j) in s:
            factrys[j] = (i + 1, s.index(str(j)))
    ss.append(s)
ss.append("X" * (w + 2))


def bfs(i):
    mp = [[None] * (w + 2) for j in range(h + 2)]
    x, y = factrys[i]
    mp[x][y] = 0
    que = deque()
    que.append((x, y))
    while True:
        (x, y) = que.popleft()
        #  print(i,x,y)
        #  print(mp[x][y])
        if (x, y) == factrys[i + 1]:
            return mp[x][y]
        count = mp[x][y]
        if ss[x + 1][y] != "X" and mp[x + 1][y] is None:
            mp[x + 1][y] = count + 1
            que.append((x + 1, y))
        if ss[x - 1][y] != "X" and mp[x - 1][y] is None:
            mp[x - 1][y] = count + 1
            que.append((x - 1, y))
        if ss[x][y + 1] != "X" and mp[x][y + 1] is None:
            mp[x][y + 1] = count + 1
            que.append((x, y + 1))
        if ss[x][y - 1] != "X" and mp[x][y - 1] is None:
            mp[x][y - 1] = count + 1
            que.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)

-    que = queue.Queue() -    que.put((x, y)) +    que = deque() +    que.append((x, y)) -        (x, y) = que.get() +        (x, y) = que.popleft() -            que.put((x + 1, y)) +            que.append((x + 1, y)) -            que.put((x - 1, y)) +            que.append((x - 1, y)) -            que.put((x, y + 1)) +            que.append((x, y + 1)) -            que.put((x, y - 1)) +            que.append((x, y - 1))