//Be Name Khoda
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma optimize O3
using namespace __gnu_pbds;
using namespace std;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
typedef long long int LL;
typedef LL ll;
typedef long double ld;
typedef string str;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef stringstream strs;
 
#define X first
#define Y second
#define PB push_back
#define For(i,a,b) for (int i=a;i<b;i++)
#define Ford(i,a,b) for (int i=a;i>=b;i--)
#define smax(a,b) a=max(a,b)
#define smin(a,b) a=min(a,b)
#define SZ(a) ((ll)a.size())
#define ER(a) cout << #a << ' ' << a << endl
#define LB(a,n,x) (lower_bound(a,(a)+(n),x)-(a))
#define RET(x) { cout << x; exit(0); } 
#define RETD(x) { cout << fixed << setprecision(15) << x; exit(0); }
const ll M=1e3+5,LG=32,SM=600+5,inf=1e18+5;
ll mod=1e9+7;

ll h,w,k;
ll xs,ys;
str jad[M];
ll dis[M][M];
ll xp[4]={0,1,0,-1};
ll yp[4]={1,0,-1,0};

int main()
{
    cin >> h >> w >> k;
    For(i,0,h)
    {
        cin >> jad[i];
        For(j,0,w)
        {
            if (jad[i][j]=='S') xs=i,ys=j;
        }
    }
    memset(dis,127,sizeof dis);
    dis[xs][ys]=0;
    // BFS
    {
        queue<pll> qu;
        qu.push({xs,ys});
        while (SZ(qu))
        {
            ll px,py;
            px=qu.front().X;
            py=qu.front().Y;
            qu.pop();
            For(r,0,4)
            {
                ll xx=px+xp[r];
                ll yy=py+yp[r];
                if (xx<0 || xx>=h || yy<0 || yy>=w) continue;
                if (jad[xx][yy]!='#' && dis[xx][yy]>dis[px][py]+1)
                {
                    dis[xx][yy]=dis[px][py];
                    qu.push({xx,yy});
                }
            }
        }
    }
    ll res=xs;
    For(i,0,h) For(j,0,w)
    {
        if (dis[i][j]<=k)
        {
            //ER(i);ER(j);
            smin(res,(ll)min(i,j));
            smin(res,h-i-1);
            smin(res,w-j-1);
        }
    }
    RET(res/k+2-!(res%k));
}    
