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

namespace ConsoleApp1 {
    class Program {
        static int h, w, k;
        static int sx, sy;
        static int min,ans;
        static string[] a;
        static int[,] map;
        static int INF = 114514;
        static int[] dx = new int[] { 1, 0, -1, 0 };
        static int[] dy = new int[] { 0, 1, 0, -1 };
        static void Main(string[] args) {
            //入力処理
            var inp = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            h = inp[0];
            w = inp[1];
            k = inp[2];
            a = new string[h];
            for (int i = 0; i < h; i++) {
                a[i] = Console.ReadLine();
            }
            Pair<int, int> tp = SearchStart();
            sx = tp.First;
            sy = tp.Second;
            //初期化
            map = new int[h, w];
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    map[i, j] = INF;
                }
            }
            map[sx, sy] = 0;
            Queue<Pair<int, int>> que = new Queue<Pair<int, int>>();
            que.Enqueue(tp);
            //処理開始
            //初回移動可能地点を列挙
            List<Pair<int, int>> li = new List<Pair<int, int>>();
            li.Add(tp);
            while (que.Count > 0) {
                Pair<int, int> nowPair = que.Dequeue();
                for (int i = 0; i < 4; i++) {
                    int nx = nowPair.First + dx[i];
                    int ny = nowPair.Second + dy[i];
                    if (0 <= nx && nx < w && 0 <= ny && ny < h && map[nx, ny] == INF&&a[nx][ny]!='#') {
                        Pair<int, int> addPair = new Pair<int, int>(nx, ny);
                        li.Add(addPair);
                        map[nx, ny] = map[nowPair.First, nowPair.Second]+1;
                        que.Enqueue(addPair);
                    }
                }
            }
            //壁までの最短を求める
            min = h * w;
            for(int i = 0; i < li.Count; i++) {
                int minX = Math.Min(li[i].First, w - 1 - li[i].First);
                int minY = Math.Min(li[i].Second, h - 1 - li[i].Second);
                int minXY = Math.Min(minX, minY);
                if (min > minXY) {
                    min = minXY;
                    
                }
                //debug
                //Console.WriteLine("x:" + li[i].First + " y:" + li[i].Second + " min:" + minXY);
            }
            //結果表示
            if (min % k == 0) {
                ans = min / k+1;
            }
            else {
                ans = min / k + 2;
            }
            Console.WriteLine(ans);

        }

        static Pair<int, int> SearchStart() {
            Pair<int, int> start = new Pair<int, int>(-1, -1);
            bool flag = false;
            for (int i = 0; i < h; i++) {
                for (int j = 0; j < w; j++) {
                    if (a[i][j] == 'S') {
                        start.First = i;
                        start.Second = j;
                        flag = true;
                        break;
                    }
                }
                if (flag) break;
            }
            return start;
        }
    }
    public class Pair<T, U> {
        public Pair(T first, U second) {
            First = first;
            Second = second;
        }
        public T First { get; set; }
        public U Second { get; set; }
    }
}
