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

static class Program
{
    class PriorityQueue<T>
    {
        private readonly List<T> m_list;
        private readonly Comparison<T> m_compare;
        public int Count { get; private set; }
        public PriorityQueue(Comparison<T> compare)
        {
            m_list = new List<T>();
            m_compare = compare;
            Count = 0;
        }
        private int Add(T value)
        {
            if (Count == m_list.Count)
            {
                m_list.Add(value);
            }
            else
            {
                m_list[Count] = value;
            }
            return Count++;
        }
        private void Swap(int a, int b)
        {
            T tmp = m_list[a];
            m_list[a] = m_list[b];
            m_list[b] = tmp;
        }
        public void Enqueue(T value)
        {
            int c = Add(value);
            while (c > 0)
            {
                int p = (c - 1) / 2;
                if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; }
                c = p;
            }
        }
        public T Dequeue()
        {
            T value = m_list[0];
            m_list[0] = m_list[--Count];
            int p = 0;
            while (true)
            {
                int c1 = p * 2 + 1;
                int c2 = p * 2 + 2;
                if (c1 >= Count) { break; }
                int c = (c2 >= Count || m_compare(m_list[c1], m_list[c2]) < 0) ? c1 : c2;
                if (m_compare(m_list[c], m_list[p]) < 0) { Swap(p, c); } else { break; }
                p = c;
            }
            return value;
        }
    }

    static int Solve(int H, int W, int K, string[] lines)
    {
        const int INF = int.MaxValue / 2;
        int T = 0, B = 0, L = 0, R = 0;
        var memo = new int[H][];
        var queue = new PriorityQueue<Tuple<int, int>>((a, b) => memo[a.Item1][a.Item2].CompareTo(memo[b.Item1][b.Item2]));
        for (int i = 0; i < H; ++i)
        {
            memo[i] = Enumerable.Repeat(INF, W).ToArray();
            for (int j = 0; j < W; ++j)
            {
                if (lines[i][j] == 'S')
                {
                    memo[i][j] = 0;
                    queue.Enqueue(Tuple.Create(i, j));
                    T = i;
                    B = i;
                    L = j;
                    R = j;
                }
            }
        }
        while (queue.Count > 0)
        {
            var t = queue.Dequeue();
            int h1 = t.Item1;
            int w1 = t.Item2;
            foreach (var u in new[] { Tuple.Create(h1 - 1, w1),
                                      Tuple.Create(h1 + 1, w1),
                                      Tuple.Create(h1, w1 - 1),
                                      Tuple.Create(h1, w1 + 1), })
            {
                int h2 = u.Item1;
                int w2 = u.Item2;
                if (0 <= h2 && h2 < H && 0 <= w2 && w2 < W)
                {
                    if (lines[h2][w2] != '#' && memo[h2][w2] > memo[h1][w1] + 1)
                    {
                        memo[h2][w2] = memo[h1][w1] + 1;
                        queue.Enqueue(Tuple.Create(h2, w2));
                        if (memo[h2][w2] <= K)
                        {
                            T = Math.Min(h2, T);
                            B = Math.Max(h2, B);
                            L = Math.Min(w2, L);
                            R = Math.Max(h2, R);
                        }
                    }
                }
            }
        }
        return (new[] { T, H - B - 1, L, W - R - 1 }.Min() + K - 1) / K + 1;
    }

    static void Main(string[] args)
    {
        var V = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
        int H = V[0], W = V[1], K = V[2];
        var lines = new string[H];
        for (int i = 0; i < H; ++i) { lines[i] = Console.ReadLine(); }
        Console.WriteLine(Solve(H, W, K, lines));
    }
}
