#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;

constexpr int Mod = 998244353;
constexpr int mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;

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

/*-------------------------------------------*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    int h, w, k;
    cin >> h >> w >> k;
    bool ng[h][w] = {};
    queue<pair<int, int>> que;
    vector<vector<int>> dis(h, vector<int>(w, -1));
    rep(i, h) rep(j, w) {
        char c;
        cin >> c;
        if(c == 'S') {
            que.emplace(i, j);
            dis[i][j] = 0;
        }
        if(c == '#') ng[i][j] = true;
    }
    int ans = 1e9;
    const int dx[] = {0, 1, 0, -1};
    const int dy[] = {1, 0, -1, 0};
    while(!que.empty()) {
        int x = que.front().first, y = que.front().second;
        que.pop();
        chmin(ans, x);
        chmin(ans, h - 1 - x);
        chmin(ans, y);
        chmin(ans, w - 1 - y);
        if(dis[x][y] >= k) continue;
        rep(i, 4) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= h || ny < 0 || ny >= w || ng[nx][ny]) continue;
            dis[nx][ny] = dis[x][y] + 1;
            que.emplace(nx, ny);
        }
    }
    cout << (ans + k - 1) / k + 1 << endl;

    return 0;
}