#include <bits/stdc++.h>
#define WHOLE(v) (v).begin(), (v).end()
#define REV_WHOLE(v) (v).rbegin(), (v).rend()
using i64 = int64_t;
using namespace std;
template<size_t I,class H,class...T>struct TupleReaderWriter{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple_cat(tuple<H>(v),TupleReaderWriter<sizeof...(T)-1,T...>::r(i));}static void w(ostream&o,tuple<H,T...>&t,string d){TupleReaderWriter<I-1,H,T...>::w(o,t,d);o<<d<<get<I>(t);}};
template<class H,class...T>struct TupleReaderWriter<0,H,T...>{static tuple<H,T...> r(istream&i){H v;i>>v;return tuple<H>(v);}static void w(ostream&o,tuple<H,T...>&t,string d){o<<get<0>(t);}};
template<class...T>istream&operator>>(istream&i,tuple<T...>&t){t=TupleReaderWriter<sizeof...(T),T...>::r(i);return i;}
template<class...T>ostream&operator<<(ostream&o,tuple<T...>&t){string delim=" ";TupleReaderWriter<sizeof...(T)-1,T...>::w(o,t,delim);return o;}
template<class T>istream&operator>>(istream&i,vector<T>&v){for(auto&x:v)i>>x;return i;}
template<class T>ostream&operator<<(ostream&o,vector<T>&v){string delim="";for(auto&x:v)o<<delim<<x,delim=" ";return o;}
template<class T>using rev_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>using vector2d=vector<vector<T>>;
struct fixprec{int x;fixprec(int d):x(d){}};
ostream&operator<<(ostream&o,fixprec f){return o<<fixed<<setprecision(f.x);}
void R_YESNO(bool p) { cout << (p ? "YES" : "NO") << endl; }
void R_YesNo(bool p) { cout << (p ? "Yes" : "No") << endl; }
int dir[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

/*!* [intutil] *!*/
/*+* 整数の演算 *+*/
template<typename T>
T divceil(T a, T b) { return a / b + (a > 0 && a % b ? 1 : 0); }
i64 gcd(i64 a, i64 b) {
    i64 c;
    while(a) c = a, a = b%a, b = c;
    return b;
}
i64 lcm(i64 a, i64 b) { return a / gcd(a, b) * b; }
// a を b で割り切る回数
int divtimes(i64 a, i64 b) { for(int i = 0;;a /= b,i++) if(a%b) return i; }


using P = pair<int, int>;

int main() {
    int H, W, K;
    cin >> H >> W >> K;
    vector2d<bool> a(H, vector<bool>(W));
    int sy, sx;
    for(int i = 0; i < H; i++) {
        string S;
        cin >> S;
        for(int j = 0; j < W; j++) {
            a[i][j] = S[j] == '#';
            if(S[j] == 'S') sy = i, sx = j;
        }
    }
    queue<pair<P, int>> q;
    q.push({{sy, sx}, 0});
    set<P> s = {{sy, sx}};
    while(q.size()) {
        auto t = q.front(); q.pop();
        int y, x;
        tie(y, x) = t.first;
        if(t.second >= K) continue;
        for(int d = 0; d < 4; d++) {
            int ny = y + dir[d][0], nx = x + dir[d][1];
            if (0 <= ny && ny < H && 0 <= nx && nx < W) {
                if(s.count({ny, nx}) || a[ny][nx]) continue;
                decltype(t) t_ = {{ny, nx}, t.second + 1};
                q.push(t_);
                s.insert(t_.first);
            }
        }
    }
    const int INF = 1e7;
    int ans = INF;
    for(auto coord : s) {
        int y, x;
        tie(y, x) = coord;
        ans = min(ans, 1 +
            min({
                divceil(y, K),
                divceil(x, K),
                divceil(H - 1 - y, K),
                divceil(W - 1 - x, K)
            })
        );
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 30208