#include<bits/stdc++.h>
using namespace std;
int x[] = {-1,0,1,0};
int y[] = {0,1,0,-1};
int h, w, k;
int main(){
  cin >> h >> w >> k;
  int si, sj;
  vector<vector<int>> dist(h, vector<int> (w,1e9));
  vector<string> a(h);
  for(int i=0; i<h; i++){
    cin >> a[i];
    for(int j=0; j<w; j++){
      if(a[i][j] == 'S'){
        si = i;
        sj = j;
        dist[i][j] = 0;
        break;
      }
    }
  }
  int maxlen = 1000;
  queue<pair<int,int>> que;
  que.push({si, sj});
  int nowi, nowj;
  while(!que.empty()){
    tie(nowi, nowj) = que.front();
    que.pop();
    maxlen = min({maxlen, nowi, nowj, h-1-nowi, w-1-nowj});
    if(dist[nowi][nowj] > k) break;
    for(int t=0; t<4; t++){
      if(clamp(nowi+x[t], 0, h-1) == nowi+x[t] && clamp(nowj+y[t], 0, w-1) == nowj+y[t]){
        if(a[nowi+x[t]][nowj+y[t]] == '#') continue;
        if(dist[nowi+x[t]][nowj+y[t]] > dist[nowi][nowj] + 1){
          dist[nowi+x[t]][nowj+y[t]] = dist[nowi][nowj] + 1;
          que.push({nowi+x[t], nowj+y[t]});
        }
      }
    }
  }
  cout << 1 + (maxlen + k - 1) / k << endl;
}