// Slower C++ solution:
#include <cstdlib>

#include <cstdio>

#include <cstring>

#include <cmath>

#include <climits>

#include <queue>

#include <iostream>





#define MAX_H 1100

#define MAX_W 1100





struct status

{

  int x;

  int y;

  int step;

  int n;

};





int dx[4]={1,0,-1,0};

int dy[4]={0,1,0,-1};



  

int H,W,N;

char maze[MAX_H][MAX_W];

int flag[MAX_H][MAX_W];

int xs,ys;

std::queue<status> Q;





int BFS();



int main(int argc, char* argv[])

{

  //freopen("in.txt","r",stdin);

  while(std::cin>>H>>W>>N)

    {



      std::cin.get();

      //std::cout << H << W << N << std::endl;



      for(int i = 0 ; i < H ; i++)

	std::cin.getline(maze[i],MAX_W);



      //for(int i = 0 ; i < H ; i++)

      //	std::cout << maze[i] << std::endl;



      for(int j = 0 ; j < H ; j++)

	for(int i = 0 ; i < W ; i++)

	  if(maze[j][i] == 'S' )

	    {

	      xs = i;

	      ys = j;

	      break;

	    }

      memset(flag,0,sizeof(flag));

      std::cout<<BFS()<<std::endl;

      

    }

}





int BFS()

{



  while(Q.empty()==false)

    Q.pop();

  status start;

  start.x = xs;

  start.y = ys;

  start.step = 0;

  start.n = 1;



  Q.push(start);



  while(Q.empty() == false )

    {

      status tmp = Q.front();

      Q.pop();





      for(int i = 0 ; i < 4 ; i++)

	{

	  int cx = tmp.x + dx[i];

	  int cy = tmp.y + dy[i];

	  int cstep = tmp.step + 1;

	  int cn = tmp.n;



	  if(cx < 0 || cx >= W ) continue;

	  if(cy < 0 || cy >= H ) continue;

	  if(maze[cy][cx] == 'X' ) continue;



	  if( (maze[cy][cx] - '0') == tmp.n )

	    cn = tmp.n + 1;

	  else

	    cn = tmp.n;



	  if(cn == N+1)

	    return cstep;



	  

	  

	  if(flag[cy][cx] >= cn  ) continue;



	  flag[cy][cx] = cn;



	  status nstatus;

	  nstatus.x = cx;

	  nstatus.y = cy;

	  nstatus.n = cn;

	  nstatus.step = cstep;

	  Q.push(nstatus);

	  

	}

    }



  return -1;

  

}

// Faster C++ solution for the same problem (measured speedup: 1.1691579650350723x):
#include <cstdio>

#include <algorithm>

#include <iostream>

#include <cstring>

#include <queue>



using namespace std;



const int hw_max = 1000 + 5;

char graph[hw_max][hw_max];

int sign[hw_max][hw_max];

int h, w, n, ans;

int dx[4] = {-1, 1, 0, 0};

int dy[4] = {0, 0, -1, 1};

struct node

{

    int x;

    int y;

    int time;

} start;



void BFS(node op, char ed)

{

    queue <node> process;

    process.push(op);

    sign[op.x][op.y] = 1;

    while(!process.empty())

    {

        node now = process.front();

        process.pop();

        for(int i = 0; i < 4; i ++)

        {

            node next = now;

            next.x += dx[i];

            next.y += dy[i];

            next.time ++;

            if(next.x >= 0 && next.x < h && next.y >= 0 && next.y < w && graph[next.x][next.y] != 'X' && !sign[next.x][next.y])

            {

                if(graph[next.x][next.y] == ed)

                {

                    ans += next.time;

                    next.time = 0;

                    start = next;

                        return;

                }

                else

                {

                    process.push(next);

                    sign[next.x][next.y] = 1;

                }

            }

        }

    }

}



int main()

{

    while(~scanf("%d %d %d", & h, & w, & n))

    {

        getchar();

        for(int i = 0; i < h; i ++)

        {

            for(int j = 0; j < w; j ++)

            {

                   scanf("%c", & graph[i][j]);

                   if(graph[i][j] == 'S')

                   {

                    start.x = i;

                    start.y = j;

                    start.time = 0;

                   }

            }

            getchar();

        }

        ans = 0;

        for(int i = 1; i <= n; i ++)

        {

            memset(sign, 0, sizeof(sign));

            BFS(start, '0' + i);

        }

        printf("%d\n", ans);

    }

    return 0;

}