#!/usr/bin/env python

# -*- coding: utf-8 -*-



import heapq

import sys



def check_out_of_map(pos,H,W):

    H = len(Map)

    W = len(Map[0])



    if pos[0] < 0 or pos[1] < 0:

        return False

    if pos[0] >= H or pos[1] >= W:

        return False

    return True



def bfs(s,g,Map,H,W):

    queue = []

    queue.append(s)

    dist = [[-1 for i in range(W)] for j in range(H)]

    direct = [(1,0),(-1,0),(0,1),(0,-1)]

    count = 0

    dist[s[0]][s[1]] = 0



    while queue != []:

        q = queue.pop(0)

        if q == g:

            return dist[q[0]][q[1]]

        for i in range(4):

            nextPos = (q[0]+direct[i][0],q[1]+direct[i][1])

            if check_out_of_map(nextPos,H,W) and Map[nextPos[0]][nextPos[1]] != "X":

                if dist[nextPos[0]][nextPos[1]] == -1:

                    dist[nextPos[0]][nextPos[1]] = dist[q[0]][q[1]] + 1

                    queue.append(nextPos)



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

cheese_queue = []

Map = []

for i in range(H):

    count = 0

    line = []

    while True:

        char = sys.stdin.read(1)

        if char == "S":

            s = (i,count)

        elif char.isnumeric():

            heapq.heappush(cheese_queue,(int(char),(i,count)))

        elif char == "\n":

            break

        line.append(char)

        count += 1

    Map.append(line)

count = 0

while cheese_queue != []:

    q = heapq.heappop(cheese_queue)

    count += bfs(s,q[1],Map,H,W)

    s = q[1]

print(count)
#!/usr/bin/env python

# -*- coding: utf-8 -*-



import heapq

import sys



def check_out_of_map(pos,H,W):

    if pos[0] < 0 or pos[1] < 0:

        return False

    if pos[0] >= H or pos[1] >= W:

        return False

    return True



def bfs(s,g,Map,H,W):

    queue = []

    queue.append(s)

    dist = [[-1 for i in range(W)] for j in range(H)]

    direct = [(1,0),(-1,0),(0,1),(0,-1)]

    count = 0

    dist[s[0]][s[1]] = 0



    while queue != []:

        q = queue.pop(0)

        if q == g:

            return dist[q[0]][q[1]]

        for i in range(4):

            nextPos = (q[0]+direct[i][0],q[1]+direct[i][1])

            if check_out_of_map(nextPos,H,W) and Map[nextPos[0]][nextPos[1]] != "X":

                if dist[nextPos[0]][nextPos[1]] == -1:

                    dist[nextPos[0]][nextPos[1]] = dist[q[0]][q[1]] + 1

                    queue.append(nextPos)



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

cheese_queue = []

Map = []

for i in range(H):

    count = 0

    line = []

    while True:

        char = sys.stdin.read(1)

        if char == "S":

            s = (i,count)

        elif char.isnumeric():

            heapq.heappush(cheese_queue,(int(char),(i,count)))

        elif char == "\n":

            break

        line.append(char)

        count += 1

    Map.append(line)

count = 0

while cheese_queue != []:

    q = heapq.heappop(cheese_queue)

    count += bfs(s,q[1],Map,H,W)

    s = q[1]

print(count)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import sys


def check_out_of_map(pos, H, W):
    H = len(Map)
    W = len(Map[0])
    if pos[0] < 0 or pos[1] < 0:
        return False
    if pos[0] >= H or pos[1] >= W:
        return False
    return True


def bfs(s, g, Map, H, W):
    queue = []
    queue.append(s)
    dist = [[-1 for i in range(W)] for j in range(H)]
    direct = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    count = 0
    dist[s[0]][s[1]] = 0
    while queue != []:
        q = queue.pop(0)
        if q == g:
            return dist[q[0]][q[1]]
        for i in range(4):
            nextPos = (q[0] + direct[i][0], q[1] + direct[i][1])
            if check_out_of_map(nextPos, H, W) and Map[nextPos[0]][nextPos[1]] != "X":
                if dist[nextPos[0]][nextPos[1]] == -1:
                    dist[nextPos[0]][nextPos[1]] = dist[q[0]][q[1]] + 1
                    queue.append(nextPos)


H, W, N = list(map(int, input().split(" ")))
cheese_queue = []
Map = []
for i in range(H):
    count = 0
    line = []
    while True:
        char = sys.stdin.read(1)
        if char == "S":
            s = (i, count)
        elif char.isnumeric():
            heapq.heappush(cheese_queue, (int(char), (i, count)))
        elif char == "\n":
            break
        line.append(char)
        count += 1
    Map.append(line)
count = 0
while cheese_queue != []:
    q = heapq.heappop(cheese_queue)
    count += bfs(s, q[1], Map, H, W)
    s = q[1]
print(count)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
import sys


def check_out_of_map(pos, H, W):
    if pos[0] < 0 or pos[1] < 0:
        return False
    if pos[0] >= H or pos[1] >= W:
        return False
    return True


def bfs(s, g, Map, H, W):
    queue = []
    queue.append(s)
    dist = [[-1 for i in range(W)] for j in range(H)]
    direct = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    count = 0
    dist[s[0]][s[1]] = 0
    while queue != []:
        q = queue.pop(0)
        if q == g:
            return dist[q[0]][q[1]]
        for i in range(4):
            nextPos = (q[0] + direct[i][0], q[1] + direct[i][1])
            if check_out_of_map(nextPos, H, W) and Map[nextPos[0]][nextPos[1]] != "X":
                if dist[nextPos[0]][nextPos[1]] == -1:
                    dist[nextPos[0]][nextPos[1]] = dist[q[0]][q[1]] + 1
                    queue.append(nextPos)


H, W, N = list(map(int, input().split(" ")))
cheese_queue = []
Map = []
for i in range(H):
    count = 0
    line = []
    while True:
        char = sys.stdin.read(1)
        if char == "S":
            s = (i, count)
        elif char.isnumeric():
            heapq.heappush(cheese_queue, (int(char), (i, count)))
        elif char == "\n":
            break
        line.append(char)
        count += 1
    Map.append(line)
count = 0
while cheese_queue != []:
    q = heapq.heappop(cheese_queue)
    count += bfs(s, q[1], Map, H, W)
    s = q[1]
print(count)

-    H = len(Map) -    W = len(Map[0])