#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=mod*mod*3LL;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
struct node{ll i, j, dis;};
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll h, w, m, sx, sy;
    cin >> h >> w >> m;
    string s[h];

    for(int i=0;i<h;i++){
    	cin >> s[i];
    	for(int j=0;j<w;j++){
    		if(s[i][j] == 'S') sx = j, sy = i;
    	}
    }

    ll dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
    ll ans = 1e9;

    queue<node> q;
    q.push({sy, sx, 0});
    bool used[h][w];
    memset(used,0,sizeof(used));
    used[sy][sx] = true;
    while(!q.empty()){
    	node now = q.front(); q.pop();
    	ans = min({ans, now.i, now.j, w - now.j - 1, h - now.i - 1});
    	if(now.dis == m) continue;
    	for(int k=0;k<4;k++){
    		ll y = now.i + dy[k];
    		ll x = now.j + dx[k];
    		if(x < 0 || y < 0 || x >= w || y >= h || s[y][x] == '#' || used[y][x]) continue;
    		used[y][x] = true;
    		q.push({y, x, now.dis + 1});
    	}
    }
    cout << (ans + m * 2 - 1) / m << endl;
    return 0;
}
