using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Contest
{
    class Program
    {
        static int Main(string[] args)
        {
            new Program().Process();

            return 0;
        }

        private CommonInput cin;

        private void Process()
        {
            this.cin = new CommonInput();

            int h, w, n;
            int sh, sw, result;
            this._map = new int[1000, 1000];
            this._qP = new Queue<Point>();

            string line;

            h = this.cin.GetNextToInt();
            w = this.cin.GetNextToInt();
            n = this.cin.GetNextToInt();

            this._maxH = h;
            this._maxW = w;

            sh = sw = 0;

            for (int i = 0; i < h; i++)
            {
                line = this.cin.GetNext();
                for (int j = 0; j < line.Length; j++)
                {
                    switch (line[j])
                    {
                        case '.':
                            this._map[j, i] = 0;
                            break;
                        case 'S':
                            this._map[j, i] = 0x10;
                            sh = i;
                            sw = j;
                            break;
                        case 'X':
                            this._map[j, i] = 0x20;
                            break;
                        default:
                            this._map[j, i] = int.Parse(line[j].ToString());
                            break;
                    }
                }
            }

            result = this.Execute(n, new Point(sw, sh));

            Console.WriteLine("{0}", result);
        }

        private int[,] _map;
        private int _maxH;
        private int _maxW;
        private Queue<Point> _qP;

        public int Execute(int N, Point start)
        {
            int c, j;
            Point end;
            int count = 0;

            this._qP.Enqueue(start);

            for (int a = 1; a <= N; a++)
            {
                j = 1;
                count++;

                while (!this.Execute(a, j, out c, out end))
                {
                    j = c;
                    count++;
                }

                this._qP.Clear();

                this._qP.Enqueue(end);
            }

            return count;
        }

        public bool Execute(int desire, int n, out int count, out Point end)
        {
            int dw, dh;
            Point current;
            bool result = false;
            count = 0;
            end = new Point(-1, -1);

            for (int a = 0; a < n; a++)
            {
                current = this._qP.Dequeue();

                for (int i = 0; i < 4; i++)
                {
                    dw = (int)Math.Cos(90 * i * Math.PI / 180);
                    dh = (int)Math.Sin(90 * i * Math.PI / 180);

                    if (!result && current.X + dw > -1 && current.Y + dh > -1 && current.X + dw < this._maxW && current.Y + dh < this._maxH && (this._map[current.X + dw, current.Y + dh] & 0x20) != 0x20 && (this._map[current.X + dw, current.Y + dh] & (0x01 << (5 + desire))) != (0x01 << (5 + desire)))
                    {
                        this._map[current.X + dw, current.Y + dh] |= 0x01 << (5 + desire);
                        this._qP.Enqueue(new Point(current.X + dw, current.Y + dh));

                        result |= (this._map[current.X + dw, current.Y + dh] & 0x0f) == desire;

                        if (result)
                            end = new Point(current.X + dw, current.Y + dh);

                        count++;
                    }
                }
            }

            return result;
        }
    }

    public class CommonInput
    {
        public CommonInput()
        {
            this._s = new string[0];
            this._i = 0;
        }

        public string Line { get; set; }
        private string[] _s;
        private int _i;
        private char[] _sep = { ' ' };

        public void GetLine()
        {
            this._i = 0;
            do
            {
                this.Line = Console.ReadLine();
            } while (this.Line == "");

            if (this.Line != null)
                this._s = this.Line.Split(this._sep, StringSplitOptions.RemoveEmptyEntries);
        }

        public string GetNext()
        {
            if (this._i < this._s.Length)
                return this._s[this._i++];

            this.GetLine();

            return this._s[this._i++];
        }

        public int GetNextToInt()
        {
            return int.Parse(this.GetNext());
        }

        public long GetNextToLong()
        {
            return long.Parse(this.GetNext());
        }

        public double GetNextToDouble()
        {
            return double.Parse(this.GetNext());
        }
    }
}

Measured memory_bytes: 13332