using System;
using System.Collections.Generic;

public class CSharp{
    public static void Main(){
        Kyopuro chokudai = new Kyopuro();
	    chokudai.AtCoder();
	}
}

public class Kyopuro{
    int h, w, k;
    int sx, sy;
    bool[][] g;
    int close;
    
    public void AtCoder(){
        string[] s;
        s = Console.ReadLine().Split(' ');
        h = int.Parse(s[0]);
        w = int.Parse(s[1]);
        k = int.Parse(s[2]);
        g = new bool[h][];
        int tmpx, tmpy;
        string t;
        for(int i=0; i<h; i++){
            g[i] = new bool[w];
            t = Console.ReadLine();
            for(int j=0; j<w; j++){
                if(t[j] != '#') g[i][j] = true;
                if(t[j] == 'S'){
                    sx = i;
                    sy = j;
                    tmpx = i;
                    tmpy = j;
                    if(h - 1 - i < i) tmpx = h - 1 - i;
                    if(w - 1 - j < j) tmpy = w - 1 - j;
                    close = tmpx;
                    if(tmpy < tmpx) close = tmpy;
                }
            }
        }
        Dfs(sx, sy, 0);
        if(close % k == 0) Console.WriteLine(close / k + 1);
        else Console.WriteLine(close / k + 2);
    }
    
    void Dfs(int x, int y, int z){
        g[x][y] = false;
        if(x == 0 || x == h - 1 || y == 0 || y == w - 1){
            close = 0;
            return;
        }
        int tmpx = x;
        int tmpy = y;
        if(h - 1 - x < x) tmpx = h - 1 - x;
        if(w - 1 - y < y) tmpy = w - 1 - y;
        if(tmpx < close){
            close = tmpx;
            if(tmpy < tmpx) close = tmpy;
        }
        if(tmpy < close) close = tmpy;
        
        if(z<k){
            if(g[x-1][y]) Dfs(x-1, y, z+1);
            if(g[x+1][y]) Dfs(x+1, y, z+1);
            if(g[x][y-1]) Dfs(x, y-1, z+1);
            if(g[x][y+1]) Dfs(x, y+1, z+1);
        }
    }
    
    public static void DebugWL(object x){ System.Diagnostics.Debug.WriteLine(x); }
}