#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define scai(x) scanf("%d", &x)
#define scac(x) scanf("%c", &x)
#define sca2i(x, y) scanf("%d %d", &x, &y)
#define sca2f(x, y) scanf("%lf %lf", &x, &y)
#define pri(x) printf("%d", x)
#define prc(x) printf("%c", x)
#define pri2(x,y) printf("%d %d", x, y);
#define prif(x) printf("%lf", x)
#define prif2(x, y) printf("%lf %lf", x, y)
#define mset(x, y) memset(x, y, sizeof(x))
#define fori(i, x, y) for(int i = x; i < y; i++)
#define pii pair<int, int>
#define pic pair<int, char>
#define pdd pair<double, double>
typedef long long ll;
#define INF 0x3f3f3f3f
#define MAX_N  1003
char m[MAX_N][MAX_N];
int dp[MAX_N][MAX_N];
//int dp[MAX_N];

int H, W, N;

int hm[4] = {-1, 1, 0, 0};
int wm[4] = {0, 0, -1, 1};
int ans = 0;

void solve(int h, int w)
{
    for(int i = 1; i <= H; i++)
        for(int j = 1; j <= W; j++)
            dp[i][j] = INF;
    dp[h][w] = 0;
    bool flag = 0;
    m[h][w] = '.';
    int t = 0;
    pii sx;
    sx.first = h;
    sx.second = w;

    ans = 0;
    queue<pii> que;
    que.push(sx);
    int n = 0;
    int NN = 1;
    while(!que.empty())
    {

        flag = 0;

       // printf("que is front is %d %d\n", que.front().first, que.front().second);
        for(int i = 0; i < 4; i++)
        {

                pii tmp;
                tmp.first = que.front().first + hm[i];
                tmp.second = que.front().second + wm[i];

                //printf("BFS is at %d %d %c\n", tmp.first, tmp.second, m[tmp.first][tmp.second]);

                if(1 <= tmp.first && tmp.first <= H && 0 < tmp.second && tmp.second <= W && m[tmp.first][tmp.second] != 'X' && dp[tmp.first][tmp.second] == INF)
                {
                    //----------------------------------------------------------
                    //printf("进入第一层if判断，基本条件成立\n");

                    if(m[tmp.first][tmp.second] == '.')
                    {
                        que.push(tmp);
                        dp[tmp.first][tmp.second] = dp[que.front().first][que.front().second] + 1;
                        //printf("BFS is at %d %d\n", tmp.first, tmp.second);
                        //t++;
                    }
                    else if(m[tmp.first][tmp.second] - '0' <= NN)
                    {
                        NN++;

                        dp[tmp.first][tmp.second] = dp[que.front().first][que.front().second] + 1;
                        ans = dp[tmp.first][tmp.second];
                        //printf("这次的步数是: %d 到的位置是%d %d 里面是%c NN 是%d\n", ans, tmp.first, tmp.second,m[tmp.first][tmp.second], NN);
                        m[tmp.first][tmp.second] = '.';
                        for(int ii = 1; ii <= H; ii++)
                            for(int jj = 1; jj <= W; jj++)
                                    dp[ii][jj] = INF;
                        dp[tmp.first][tmp.second] = ans;
                        while(!que.empty())
                        {
                            que.pop();
                            //t++;

                        }
                        flag = 1;
                        que.push(tmp);
                    }
                    else
                    {
                        que.push(tmp);
                        dp[tmp.first][tmp.second] = dp[que.front().first][que.front().second] + 1;
                        //t++;
                    }
                }
        }
        if(!flag)
            que.pop();
    }
    /*for(int i = 1; i <= H; i++){
            puts("");
        for(int j = 1; j <= W; j++)
            {
                printf("%c", m[i][j]);
            }
    }*/
    pri(ans);
    puts("");
}
int main()
{
    scanf("%d %d %d", &H, &W, &N);
    for(int i = 1; i <= H; i++)
    {
        getchar();
        for (int j = 1; j <= W; j++)
        {
            scac(m[i][j]);
        }
    }
    for(int i = 1; i <= H; i++)
    {
        //getchar();
        for (int j = 1; j <= W; j++)
        {
            if (m[i][j] == 'S')
            {
                solve(i, j);
                break;
            }
        }
    }
    //system("pause");
    return 0;

}


Measured memory_bytes: 8060