#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF= 1000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<ll,ll> P;

template<typename T>
T Pow(T a,T b) {
    T ret = 1;
    for(int i = 0;i < b;i++) {
        ret *= a;
    }
    return ret;
}
 
ll mod(ll val) {
    ll res = val % MOD;
    if(res < 0) {
        res += MOD;
    }
    return res;
}
 
ll RS(ll N, ll P, ll M){
    if(P==0) return 1;
    if(P < 0) {
        return 0;
    }
    if(P%2==0){
        ll t = RS(N, P/2, M);
        return t*t % M;
    }
    return N * RS(N, P-1, M) % M;
}


int H,W,K;
int sy,sx;
char maze[880][880];
int d[880][880];
vector<int> dy = {1,-1,0,0};
vector<int> dx = {0,0,-1,1};

void bfs1() {
    queue<pair<P,int>> que;
    que.push(make_pair(P(sy,sx),0));
    d[sy][sx] = 0;
    while(!que.empty()) {
        int cost = que.front().second;
        P Coordinate = que.front().first;
        que.pop();
        if(cost == K) {
            continue;
        }
        for (int i = 0; i < 4; ++i)
        {
            int ny = Coordinate.first + dy.at(i);
            int nx = Coordinate.second + dx.at(i);
            if(0 <= ny && ny < H && 0 <= nx && nx < W && maze[ny][nx] != '#') {
                if(d[ny][nx] > d[Coordinate.first][Coordinate.second] + 1) {
                    d[ny][nx] = d[Coordinate.first][Coordinate.second] + 1;
                    que.push(make_pair(P(ny,nx),cost + 1));
                }
            }
        }
    }
}

void bfs2() {
    queue<P> que;
    for (int i = 0; i < H; ++i)
    {
        for (int j = 0; j < W; ++j)
        {
            if(d[i][j] == 0) {
                que.push(P(i,j));
            }
        }
    }
    while(!que.empty()) {
        P Coordinate = que.front();
        que.pop();
        for (int i = 0; i < 4; ++i)
        {
            int ny = Coordinate.first + dy.at(i);
            int nx = Coordinate.second + dx.at(i);
            if(0 <= ny && ny < H && 0 <= nx && nx < W) {
                if(d[ny][nx] > d[Coordinate.first][Coordinate.second] + 1) {
                    d[ny][nx] = d[Coordinate.first][Coordinate.second] + 1;
                    que.push(P(ny,nx));
                }
            }
        }
    }
}

int main() {
    cin >> H >> W >> K;
    for (int i = 0; i < H; ++i)
    {
        for (int j = 0; j < W; ++j)
        {
            cin >> maze[i][j];
            if(maze[i][j] == 'S') {
                sy = i;
                sx = j;
            }
        }
    }
    for (int i = 0; i < 880; ++i)
    {
        for (int j = 0; j < 880; ++j)
        {
            d[i][j] = Inf;
        }
    }
    bfs1();
    for (int i = 0; i < H; ++i)
    {
        for (int j = 0; j < W; ++j)
        {
            if(d[i][j] != Inf) {
                d[i][j] = 0;
            }
        }
    }
    bfs2();
    int ret = Inf;
    for (int i = 0; i < H; ++i)
    {
        ret = min(ret,(d[i][0] + K - 1) / K + 1);
        ret = min(ret,(d[i][W - 1] + K - 1) / K + 1);
    }
    for (int i = 0; i < W; ++i)
    {
        ret = min(ret,(d[0][i] + K - 1) / K + 1);
        ret = min(ret,(d[H - 1][i] + K - 1) / K + 1);
    }
    cout << ret << endl;
}

Measured memory_bytes: 17396