#include<bits/stdc++.h>
using namespace std;
int H, W, K;
int MAP[1000][1000];
int x, y, vis[1000][1000];
int ans = 2147483647;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
struct PR {
    int x, y, now;
    PR() {x = y = now = 0;}
    PR(int _x, int _y, int _now) {x = _x, y = _y, now = _now;}
};
bool check(int x, int y) {
    if (x < 0 || x > H) return 0;
    if (y < 0 || y > W) return 0;
    if (MAP[x][y]) return 0;
    return 1;
}
int main() {
    cin >> H >> W >> K;
    for (int i = 1; i <= H; i ++) {
        string s;
        cin >> s;
        for (int j = 1; j <= W; j ++) {
            if (s[j - 1] == '#') MAP[i][j] = 1;
            else {
                MAP[i][j] = 0;
                if (s[j - 1] == 'S') x = i, y = j; 
            }
        }
    }
    queue<PR> Q; Q.push((PR){x, y, 0});
    while (!Q.empty()) {
        PR tmp = Q.front(); Q.pop();
        if (tmp.now > K) continue;
        if (vis[tmp.x][tmp.y] <= tmp.now && vis[tmp.x][tmp.y] != 0) continue;
        vis[tmp.x][tmp.y] = tmp.now;
		int num = min(min(H - tmp.x, W - tmp.y), min(tmp.x - 1, tmp.y - 1));
		tmp = (tmp + K - 1) / K;
		if (tmp + 1 > ans) continue;
		ans = min(ans, tmp + 1);
        for (int i = 0; i < 4; i ++) {
            if (check(tmp.x + dx[i], tmp.y + dy[i])) Q.push((PR){tmp.x + dx[i], tmp.y + dy[i], tmp.now + 1});
        }
    }
    cout << ans << endl;
    return 0;
}