#include <iostream>
#include <vector>
#include <array>
#include <queue>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class F> auto recur(F f) {return [f](auto... a){ return f(f, a...);};}
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }

using P = array<int, 2>;
const P d4[4] = {
    {1, 0},
    {0, 1},
    {-1, 0},
    {0, -1},
};

int h, w;

bool bc(int x, int y) {
    return 0 <= x && x < w && 0 <= y && y < h;
}
P move(int x, int y, int dir) {
    int nx, ny;
    nx = x + d4[dir][0];
    ny = y + d4[dir][1];
    return P{nx, ny};
}


int main() {
    int k;
    cin >> h >> w >> k;
    cout << h << " " << w << " " << k << endl;
    V<string> mp(h);
    int sx = -1, sy = -1;
    for (int i = 0; i < h; i++){
        cin >> mp[i];
        for (int j = 0; j < w; j++) {
            if (mp[i][j] == 'S') {
                sx = j; sy = i;
            }
        }
    }
    auto dist2 = [&](int x, int y) {
        return min(min(x, w-1-x), min(y, h-1-y));
    };
    VV<int> dist = VV<int>(h, V<int>(w, TEN(9)));
    VV<bool> used = VV<bool>(h, V<bool>(w, false));
    queue<P> q;
    q.push(P{sx, sy});
    dist[sy][sx] = 0;
    int ans = TEN(9);
    while (!q.empty()) {
        P p = q.front(); q.pop();
        int x = p[0], y = p[1];
        if (used[y][x]) continue;
        if (dist[y][x] > k) continue;
        used[y][x] = true;
        ans = min(ans, 1+(dist2(x, y)+k-1)/k);
        for (int d = 0; d < 4; d++) {
            auto np = move(x, y, d);
            int nx = np[0], ny = np[1];
            if (bc(nx, ny) == false) continue;
            if (mp[ny][nx] == '#') continue;
            if (dist[ny][nx] < dist[y][x]+1) continue;
            dist[ny][nx] = dist[y][x]+1;
            q.push(P{nx, ny});
        }
    }
    cout << ans << endl;
    return 0;
}


