Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        h, w, n = map(int, raw_input().split())
        board = [[None for j in range(w+1)] for i in range(h+1)]
        s = (None, None)
        nxt = 1
        ans = 0
        for i in range(1, h+1):
            row = raw_input()
            for j in range(1, w+1):
                board[i][j] = row[j-1]
                if board[i][j] == 'S':
                    s = (i, j, ans)
        # print board
        # print s
        # print board[3][3]
        d = [(0, 1), (1, 0), (0, -1), (-1, 0)]

        # print nxt
        q = [s]
        while nxt <= n:
            while q != []:
                y, x, ans = q.pop(0)
                # print y, x, ans, nxt
                # if (y, x) == (3, 3):
                #     print nxt
                #     print board[y][x]
                #     print nxt == board[y][x]
                if board[y][x] == str(nxt):
                    nxt += 1
                    q = [(y, x, ans)]
                    # print 'nxt: ' + str(nxt)
                    break
                ans += 1
                for dy, dx in d:
                    if 0 < y + dy <= h and 0 < x + dx <= w and board[y+dy][x+dx] != 'X':
                        q.append((y + dy, x + dx, ans))
        print ans
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        h, w, n = map(int, raw_input().split())
        board = [[None for j in range(w+1)] for i in range(h+1)]
        s = (None, None)
        ans = 0
        for i in range(1, h+1):
            row = raw_input()
            for j in range(1, w+1):
                board[i][j] = row[j-1]
                if board[i][j] == 'S':
                    s = (i, j, ans)
                elif board[i][j] == '1':
                    g = (i, j)
        # print board
        # print s
        # print board[3][3]
        d = [(0, 1), (1, 0), (0, -1), (-1, 0)]

        used = set(s)
        # print nxt
        q = [s]
        for nxt in range(1, n+1):
            # print q
            while q != []:
                y, x, ans = q.pop(0)
                # print y, x, ans, used
                # if (y, x) == (3, 3):
                #     print nxt
                #     print board[y][x]
                #     print nxt == board[y][x]
                # if board[y][x] == str(nxt):
                #     q = [(y, x, ans)]
                #     # print 'nxt: ' + str(nxt)
                #     break
                if y == g[0] and x == g[1]:
                    break
                ans += 1
                for dy, dx in d:
                    if 0 < y + dy <= h and 0 < x + dx <= w and board[y+dy][x+dx] != 'X' and (not (y + dy, x + dx) in used):
                        used.add((y + dy, x + dx))
                        q.append((y + dy, x + dx, ans))
                        
            q = [(y, x, ans)]
            used = set((y, x))            
            for i in range(1, h+1):
                for j in range(1, w+1):
                    if board[i][j] == str(nxt+1):
                        g = (i, j)
        # print q
        print ans
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Buggy python submission (verdict: Runtime Error):
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        h, w, n = map(int, raw_input().split())
        board = [[None for j in range(w+1)] for i in range(h+1)]
        s = (None, None)
        ans = 0
        for i in range(1, h+1):
            row = raw_input()
            for j in range(1, w+1):
                board[i][j] = row[j-1]
                if board[i][j] == 'S':
                    s = (i, j, ans, [])
                elif board[i][j] == '1':
                    g = (i, j)
        # print board
        # print s
        # print board[3][3]
        d = [(0, 1), (1, 0), (0, -1), (-1, 0)]

        # print nxt
        q = [s]
        for nxt in range(1, n+1):
            # print q
            while q != []:
                y, x, ans, used = q.pop(0)
                # print y, x, ans, used
                # if (y, x) == (3, 3):
                #     print nxt
                #     print board[y][x]
                #     print nxt == board[y][x]
                # if board[y][x] == str(nxt):
                #     q = [(y, x, ans)]
                #     # print 'nxt: ' + str(nxt)
                #     break
                if y == g[0] and x == g[1]:
                    break
                ans += 1
                for dy, dx in d:
                    if 0 < y + dy <= h and 0 < x + dx <= w and board[y+dy][x+dx] != 'X' and (not (y + dy, x + dx) in used):
                        q.append((y + dy, x + dx, ans, used + [(y, x)]))
                        
            q = [(y, x, ans, [])]
            for i in range(1, h+1):
                for j in range(1, w+1):
                    if board[i][j] == str(nxt+1):
                        g = (i, j)
        # print q
        print ans
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Fixed submission:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        h, w, n = map(int, raw_input().split())
        board = [[None for j in range(w+1)] for i in range(h+1)]
        s = (None, None)
        ans = 0
        for i in range(1, h+1):
            row = raw_input()
            for j in range(1, w+1):
                board[i][j] = row[j-1]
                if board[i][j] == 'S':
                    s = (i, j, ans)
                elif board[i][j] == '1':
                    g = (i, j)
        # print board
        # print s
        # print board[3][3]
        d = [(0, 1), (1, 0), (0, -1), (-1, 0)]

        used = set(s)
        # print nxt
        q = [s]
        for nxt in range(1, n+1):
            # print q
            while q != []:
                y, x, ans = q.pop(0)
                # print y, x, ans, used
                # if (y, x) == (3, 3):
                #     print nxt
                #     print board[y][x]
                #     print nxt == board[y][x]
                # if board[y][x] == str(nxt):
                #     q = [(y, x, ans)]
                #     # print 'nxt: ' + str(nxt)
                #     break
                if y == g[0] and x == g[1]:
                    break
                ans += 1
                for dy, dx in d:
                    if 0 < y + dy <= h and 0 < x + dx <= w and board[y+dy][x+dx] != 'X' and (not (y + dy, x + dx) in used):
                        used.add((y + dy, x + dx))
                        q.append((y + dy, x + dx, ans))
                        
            q = [(y, x, ans)]
            used = set((y, x))            
            for i in range(1, h+1):
                for j in range(1, w+1):
                    if board[i][j] == str(nxt+1):
                        g = (i, j)
        # print q
        print ans
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()