#include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define pb push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define all(v) ALL(v)
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
constexpr double eps = 1e-14; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/
int h,w,k;
int sx,sy;
pii stat[800*800];
int sn;

bool used[800][800];

char s[800][800];

int go(int gx,int gy){
    int ret = INF;
    for(int i=0;i<sn;i++){
        pii st =stat[i];
        int x = st.first;
        int y = st.second;
        int gdist = abs(gx-x)+abs(gy-y);
        int cnt = gdist/k;
        if(gdist%k>0) cnt++;
        ret = min(ret,cnt);
    }
    return ret;
}

int main(){
    cin >> h >> w >> k;
    rep(i,h) scanf("%s",&s[i]);

    rep(i,h) rep(j,w){
        if(s[i][j]=='S'){
            sx = i;
            sy = j;
            break;
        }
    }

    // 1回の魔法で行ける場所
    used[sx][sy]=true;
    queue<pii> que;
    que.push(pii(sx,sy));

    while(!que.empty()){
        auto p = que.front();
        que.pop();
        int x = p.first;
        int y = p.second;
        if(abs(x-sx)+abs(y-sy)>=k) continue;

        for(int i=0;i<4;i++){
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(nx<0 or ny<0 or nx>h-1 or ny>w-1) continue;
            if(used[nx][ny] or s[nx][ny]=='#') continue;
            used[nx][ny]=true;
            que.push(pii(nx,ny));
        }
    }


    rep(i,h) rep(j,w){
        if(abs(i-sx)+abs(j-sy)<=k && s[i][j]=='.' && used[i][j]){
            s[i][j]='S';
            stat[sn++] = make_pair(i,j);
        }
    }
    stat[sn++] = make_pair(sx,sy);

    //rep(i,h) rep(j,w)  if(s[i][j]=='S') stat[idx++] = make_pair(i,j);

    int ans = INF;
    //for(int i=0;i<h;i++) ans = min(ans,go(i,0));
    //for(int i=0;i<h;i++) ans = min(ans,go(i,w-1));
    //for(int i=0;i<w;i++) ans = min(ans,go(0,i));
    //for(int i=0;i<w;i++) ans = min(ans,go(h-1,i));

    for(int i=0;i<sn;i++){
        int x = stat[i].first;
        int y = stat[i].second;

        int mind=INF;
        mind=min(mind,x);   // 
        mind=min(mind,y);
        mind=min(mind,abs(w-1-y));
        mind=min(mind,abs(h-1-x));
        int cnt = mind/k;
        if(mind%k>0) cnt++;
        ans = min(ans,cnt);
    }
    cout << ans+1 << endl;
}


Measured memory_bytes: 6400