#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <utility>

using namespace std;

int main()
{
    int H, W, K;
    cin>>H>>W>>K;
    vector<string> A(H);
    for (string &a: A)
        cin>>a;

    vector<vector<int>> D(H, vector<int>(W, 99999999));
    queue<pair<int,int>> Q;
    for (int y=0; y<H; y++)
    for (int x=0; x<W; x++)
        if (A[y][x] == 'S')
            D[y][x] = 0,
            Q.push(make_pair(x, y));
    int dx[] = {-1, 1, 0, 0};
    int dy[] = {0, 0, -1, 1};
    while (!Q.empty())
    {
        int sx = Q.front().first;
        int sy = Q.front().second;
        Q.pop();
        for (int d=0; d<4; d++)
        {
            int tx = sx + dx[d];
            int ty = sy + dy[d];
            if (0<=tx && tx<W && 0<=ty && ty<H && A[ty][tx]=='.' && D[ty][tx]>D[sy][sx]+1)
                D[ty][tx] = D[sy][sx]+1,
                Q.push(make_pair(tx, ty));
        }
    }

    int ans = 99999999;
    for (int y=0; y<H; y++)
    for (int x=0; x<W; x++)
        if (D[y][x] <= K)
            ans = min(ans, (min(min(y,H-y-1),min(x,W-x-1))+K-1)/K + 1);
    cout<<ans<<endl;
}
