#include <iostream>
#include <vector>
#include <queue>
#include <unordered_set>
#include <map>
#include <algorithm>

using namespace std;

const int dy[4] = {1, 0, -1, 0};
const int dx[4] = {0, 1, 0, -1};
int H, W, K;

class Coord {
    public:
        int y, x, k;

        Coord(int i, int j, int _k) {
            y = i; x = j; k = _k;
        }
        void setNextCoord(vector<Coord>& v) {
            for (int i=0; i<4; i++) {
                if (k+1 > K) continue;
                v.push_back(Coord(y+dy[i], x+dx[i], k+1));
            }
        }
        int hash() { return y*W + x; }
        int remain() { return min(min(y, H-y-1), min(x, W-x-1)); }
};

int main() {
    cin >> H >> W >> K;
    vector<String> A(H);

    unordered_set<int> st;
    queue<Coord> que;
    for (int i=0; i<H; i++) {
        cin >> A[i];
        for (int j=0; j<W; j++) {
            if (A[i][j] == 'S') que.push(Coord(i, j, 0));
        }
    }

    int minRemain = 801;
    vector<Coord> nextCoord;
    while (!que.empty()) {
        Coord c = que.front(); que.pop();
        st.insert(c.hash());

        cout << c.y <<","<< c.x <<","<< c.k <<"/"<< c.remain() << endl;

        minRemain = c.remain() < minRemain ? c.remain() : minRemain;
        if (minRemain == 0) break;

        nextCoord.clear();
        c.setNextCoord(nextCoord);

        for (Coord nc : nextCoord) {
            if (st.find(nc.hash()) != st.end()) continue;
            if (A[nc.y][nc.x] == '#') continue;
            que.push(nc);
        }
    }

    cout << 1 + (minRemain+K-1)/K << endl;
    return 0;
}