#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(std::unique(all(x)), x.end());
typedef long long ll;
const int INF = 1e9;
const ll MOD = 1e9+7;
const ll LINF = 1e18;
const double PI=acos(-1);
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

vector<int> dy={1,0,-1,0};
vector<int> dx={0,1,0,-1};

int main() {
    int h,w,k;
    cin>>h>>w>>k;
    auto vec=make_vec<char>(h,w);
    int sy,sx;
    rep(i,h) rep(j,w) {
        cin>>vec[i][j];
        if(vec[i][j]=='S') {
            sy=i;
            sx=j;
        }
    }
    
    auto s=make_vec<bool>(h,w);
    auto checked=make_vec<bool>(h,w);
    checked[sy][sx]=true;
    queue<pair<int,int>> q;
    q.push({sy,sx});
    while(sz(q)) {
        pair<int,int> now=q.front();
        q.pop();
        s[now.first][now.second]=true;
        rep(i,4) {
            int ny = now.first + dy[i];
            int nx = now.second + dx[i];
            
            if(!(0<=ny && ny<h && 0<=nx && nx<w)) continue;
            if(checked[ny][nx]) continue;
            checked[ny][nx]=true;
            if(vec[ny][nx]=='.') q.push({ny,nx});
        }
    }

    int ans=INF;
    rep(i,h) rep(j,w) {
        if(s[i][j]) {
            chmin(ans,(i+k-1)/k);
            chmin(ans,(j+k-1)/k);
            chmin(ans,(h-1-i+k-1)/k);
            chmin(ans,(w-1-j+k-1)/k);
        }
    }
    cout<<ans+1<<endl;
}