#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <climits> 
#include <assert.h>
#include <random>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1'000'000'007;
constexpr ll mod2 = 998244353;
const double PI = 3.14159265358;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int H,W,K;
  cin >> H >> W >> K;
  vector<string> G(H);
  for(int i=0; i<H; i++) cin >> G[i];
  int sy,sx;
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      if(G[i][j] == 'S'){
        sy = i;
        sx = j;
      }
    }
  }
  queue<P> que;
  que.emplace(sy,sx);
  vector<vector<int>> dist(H,vector<int>(W,-1));
  dist[sy][sx] = 0;
  auto check = [&](int y,int x){
    return min({y,x,H-y,W-x});
  };
  int mn = inf;
  while(que.size()){
    int y,x;
    tie(y,x) = que.front();
    chmin(mn,check(y,x));
    que.pop();
    if(dist[y][x] == K) continue;
    for(int k=0; k<4; k++){
      int ny = y + dy[k];
      int nx = x + dx[k];
      if(ny<0 || ny>=H || nx<0 || nx>=W) continue;
      if(dist[ny][nx] != -1) continue;
      if(G[ny][nx] == '#') continue;
      dist[ny][nx] = dist[y][x] + 1;
      que.emplace(ny,nx);
    }
  }
  cout << (mn+K-1)/K + 1 << "\n";
  return 0;
}