#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cstring>
#include <deque>
#include <math.h>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <queue>
#include <vector>

#define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define rFOR(i, a, b) for(int (i) = (b); (i) >= (a); --(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) rFOR(i, 0, (n-1))
#define SORT(A) std::sort((A).begin(), (A).end())
#define ALL(A) (A).begin(), (A).end()
// 座標圧縮 (for vector) : ソートしてから使うのが一般的 ; SORT(A) => COORDINATE_COMPRESSION(A)
#define COORDINATE_COMPRESSION(A) (A).erase(unique((A).begin(),(A).end()),(A).end())

using lli = long long int;
using pii = std::pair<int, int>;

template <typename T>
inline bool chmin(T& a, T b){
    if(b < a){ a = b; return true;}
    return false;
}

template <typename T>
inline bool chmax(T& a, T b){
    if(a < b){ a = b; return true;}
    return false;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


constexpr int inf = 1e9 + 334;
constexpr int N = 800;
char str[N+10][N+10];
std::array<std::array<int, N>, N> Dist;
constexpr std::array<std::pair<int, int>, 4> dxdy = {pii(1, 0), pii(-1, 0), pii(0, 1), pii(0, -1)};

int main(void){
    // 入力部分
    int H, W, k; scanf("%d%d%d", &H, &W, &k);
    REP(i, H) scanf("%s", str[i]);

    // 準備部分
    int sx = -1, sy; REP(i, H){  REP(j, W) if(str[i][j] == 'S') {sx = i; sy = j; break; } if(sx != -1) break;}
    std::queue<std::pair<int, int>> Q;

    REP(i, H){
        int dx = (sx > i) ? sx - i : i - sx;
        REP(j, W){
            int dy = (sy > j) ? sy - j : j - sy;
            if(dx + dy <= k and str[i][j] == '.') {Dist[i][j] = 0; Q.emplace(i, j);}
            else Dist[i][j] = inf;
        }
    }

    Dist[sx][sy] = 0; Q.emplace(sx, sy);
    const std::function<bool(int, int)> isIn = [&](int x, int y){
        return 0 <= x and x < H and 0 <= y and y < W;
    };
    const std::function<int(int)> getDistance = [&](int d){
        return (d + k - 1) / k + 1;
    };


    while(not Q.empty()){
        int x, y; std::tie(x, y) = Q.front(); Q.pop();
        for(const auto& itr : dxdy){
            int X = x + itr.first; int Y = y + itr.second;
            if((not isIn(X, Y)) or Dist[X][Y] != inf) continue;
            Dist[X][Y] = Dist[x][y] + 1; Q.emplace(X, Y);
        }
    }
    int res = inf;
    REP(i, H) chmin(res, getDistance(Dist[i][0])), chmin(res, getDistance(Dist[i][W-1]));
    REP(j, W) chmin(res, getDistance(Dist[0][j])), chmin(res, getDistance(Dist[H-1][j]));
    printf("%d\n", res);
    
    return 0;
}
