#include <bits/stdc++.h>
using namespace std;
using pr = pair<int, int>;

int N, M, K, ans = 1e9;
int d[4] = { 1, -1, 0, 0 }, Sx, Sy;
int dist[808][808];
char A[808][808];
queue<pr> q;

int f(int x, int y) { return min({ x - 1, y - 1, N - x, M - y }); }

int main() {
    scanf("%d %d %d", &N, &M, &K);
    for(int i=1; i<=N; i++) scanf("%s", A[i] + 1);
    for(int i=1; i<=N; i++) for(int j=1; j<=M; dist[i][j] = 1e9, j++) if(A[i][j] == 'S') Sx = i, Sy = j;
    q.push(pr(Sx, Sy)), dist[Sx][Sy] = 0;
    while(!q.empty()) {
        int x = q.front().first, y = q.front().second; q.pop();
        ans = min(ans, f(x, y));
        if(dist[x][y] == K) continue;
        for(int i=0; i<4; i++) {
            int xx = x + d[i], yy = y + d[3-i];
            if(A[xx][yy] == '.' && dist[xx][yy] > dist[x][y] + 1) {
                q.push(pr(xx, yy));
                dist[xx][yy] = dist[x][y] + 1;
            }
        }
    }
    printf("%d\n", ans / K + !!(ans % K) + 1);
}
