// Slower C++ solution:


#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>



using namespace std;

const int maxn = 1e3 + 5;

const int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

int mp[maxn][maxn];

//bool vis[maxn][maxn];

int d[maxn][maxn];

struct point{

    int x,y;

};

int h,w,n;

 int dis = 0;

void bfs(point &p,int cnt)

{

   // memset(vis, 0, sizeof(vis));

    memset(d, 0, sizeof(d));

    queue<point> q;

    q.push(p);

    while (!q.empty()) {

        point t = q.front();q.pop();

        //vis[t.x][t.y] = true;

        if(mp[t.x][t.y] == cnt + 1) {dis += d[t.x][t.y];p.x = t.x,p.y = t.y;return ;}

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

            int tx = t.x + dir[i][0];

            int ty = t.y + dir[i][1];

            if (tx >= 0 && tx < h && ty >= 0 && ty < w && !d[tx][ty] && mp[tx][ty] != -1) {

                q.push({tx,ty});

                d[tx][ty] = 1 + d[t.x][t.y];

            }

        }

    }

}

int main()

{

    while (scanf("%d%d%d",&h,&w,&n) != EOF) {

        getchar();

        char ch;

        memset(mp, -1, sizeof(mp));

       // int sx,sy;

        point s;

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

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

                scanf("%c",&ch);

                if(ch == 'S') {s.x = i,s.y = j;mp[i][j] = 0;}

                else if(ch == '.') {mp[i][j] = 0;}

                else if(isdigit(ch)) {mp[i][j] = ch - '0';}

            }

            getchar();

        }

       

        dis = 0;

        for (int i = 0; i < n; i ++) {

            bfs(s,i);

        }

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

    }

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8332397997023266x):


#include<iostream>

#include<queue>

#include<string.h>

using namespace std;

char map[1005][1005];

char map1[1005][1005];

int a[1005][1005];

int m,n,q,num=0;

int main()

{

	int x,y;

	cin>>m>>n>>q;

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

	{

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

		{

			cin>>map[i][j];

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

				{

					x=i;

					y=j;

				 map[x][y]='0';

				}

		}

	}

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

		{

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

			{

				map1[i][j]=map[i][j];



			}

		}



	for(int k=1;k<=q;k++)

	{

			queue<int> ac;

			queue<int> ak;



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

		{

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

			{

				map[i][j]=map1[i][j];

				if(map1[i][j]-'0'==k-1)

				{

					x=i;

					y=j;

					map[x][y]='X';

				}

			}

		}

		memset(a,0,sizeof(a));

		ac.push(x);

		ak.push(y);

		while(!ac.empty())

		{

			x=ac.front();

			y=ak.front();

			ac.pop();

			ak.pop();

			if(x+1<m)

			{

				if(map[x+1][y]!='X')

				{

					if(map[x+1][y]-'0'==k)

					{

						num+=a[x][y];

						num+=1;

						break;

					}

						map[x+1][y]='X';

						ac.push(x+1);

						ak.push(y);

						a[x+1][y]=a[x][y]+1;

				}

			}

				if(x-1>=0)

				{

					if(map[x-1][y]!='X')

					{



						if(map[x-1][y]-'0'==k)

						{

							num+=a[x][y]+1;

							break;

						}

						map[x-1][y]='X';

						ac.push(x-1);

						ak.push(y);

						a[x-1][y]=a[x][y]+1;

					}

				}

			if(y+1<n)

			{

				if(map[x][y+1]!='X')

				{



					if(map[x][y+1]-'0'==k)

					{

						num+=a[x][y]+1;

						break;

					}



						map[x][y+1]='X';

						ac.push(x);

						ak.push(y+1);

						a[x][y+1]=a[x][y]+1;

				}

			}

			if(y-1>=0)

			{

				if(map[x][y-1]!='X')

				{



					if(map[x][y-1]-'0'==k)

					{

						num+=a[x][y]+1;

						break;

					}



						map[x][y-1]='X';

						ac.push(x);

						ak.push(y-1);

						a[x][y-1]=a[x][y]+1;

				}

			}

		}

	}

	cout<<num<<endl;

	return 0;

}