using System;
using System.Collections.Generic;

public class Program
{
    private int H, W, K;
    private char[][] A;

    private int[] nextY = new int[] {1, 0, -1, 0};
    private int[] nextX = new int[] {0, 1, 0, -1};

    public void Solve()
    {
        var sc = new Scanner();
        H = sc.NextInt();
        W = sc.NextInt();
        K = sc.NextInt();
        A = new char[H][];
        for (int i = 0; i < H; i++)
        {
            A[i] = sc.Next().ToCharArray();
        }

        int[][] dist = new int[H][];
        bool[][] f = new bool[H][];
        for (int i = 0; i < H; i++)
        {
            f[i] = new bool[W];
            dist[i] = new int[W];
            for (int j = 0; j < W; j++)
            {
                dist[i][j] = int.MaxValue;
            }
        }

        int sY = -1;
        int sX = -1;

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (A[i][j] == 'S')
                {
                    sY = i;
                    sX = j;
                }
            }
        }

        var xQ = new Queue<int>();
        var yQ = new Queue<int>();
        yQ.Enqueue(sY);
        xQ.Enqueue(sX);

        dist[sY][sX] = 0;

        while (yQ.Count > 0)
        {
            int y = yQ.Dequeue();
            int x = xQ.Dequeue();
            if (f[y][x]) continue;
            f[y][x] = true;
            int d = dist[y][x] + 1;
            for (int i = 0; i < 4; i++)
            {
                int nY = y + nextY[i];
                int nX = x + nextX[i];
                if (nY < 0 || H <= nY)
                    continue;
                if (nX < 0 || W <= nX)
                    continue;
                if (A[nY][nX] == '#')
                    continue;
                if (f[nY][nX])
                    continue;
                dist[nY][nX] = Math.Min(dist[nY][nX], d);
                xQ.Enqueue(nX);
                yQ.Enqueue(nY);
            }
        }

        int ans = int.MaxValue;

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (dist[i][j] <= K)
                {
                    int x = Math.Min(j, W - j - 1);
                    int y = Math.Min(i, H - i - 1);
                    int d = Math.Min(x, y);
                    ans = Math.Min(ans, 1 + (d + K - 1) / K);
                }
            }
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args)
    {
        new Program().Solve();
    }
}

class Scanner
{
    public Scanner()
    {
        _pos = 0;
        _line = new string[0];
    }

    const char Separator = ' ';
    private int _pos;
    private string[] _line;

    #region スペース区切りで取得

    public string Next()
    {
        if (_pos >= _line.Length)
        {
            _line = Console.ReadLine().Split(Separator);
            _pos = 0;
        }

        return _line[_pos++];
    }

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }

    #endregion

    #region 型変換

    private int[] ToIntArray(string[] array)
    {
        var result = new int[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = int.Parse(array[i]);
        }

        return result;
    }

    private long[] ToLongArray(string[] array)
    {
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = long.Parse(array[i]);
        }

        return result;
    }

    private double[] ToDoubleArray(string[] array)
    {
        var result = new double[array.Length];
        for (int i = 0; i < array.Length; i++)
        {
            result[i] = double.Parse(array[i]);
        }

        return result;
    }

    #endregion

    #region 配列取得

    public string[] Array()
    {
        if (_pos >= _line.Length)
            _line = Console.ReadLine().Split(Separator);

        _pos = _line.Length;
        return _line;
    }

    public int[] IntArray()
    {
        return ToIntArray(Array());
    }

    public long[] LongArray()
    {
        return ToLongArray(Array());
    }

    public double[] DoubleArray()
    {
        return ToDoubleArray(Array());
    }

    #endregion
}

Measured memory_bytes: 20812