#include <bits/stdc++.h>
#define int long long int
using namespace std;
template<typename T,typename U> using P=pair<T,U>;
template<typename T> using V=vector<T>;
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}

template<typename T>auto&operator<<(ostream&s,const vector<T>&v){s<<"[";bool a=1;for(auto e:v){s<<(a?"":" ")<<e;a=0;}s<<"]";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const pair<T,U>&p){s<<"("<<p.first<<","<<p.second<<")";return s;}
template<typename T>auto&operator<<(ostream&s,const set<T>&st){s<<"{";bool a=1;for(auto e:st){s<<(a?"":" ")<<e;a=0;}s<<"}";return s;}
template<typename T,typename U>auto&operator<<(ostream&s,const map<T,U>&m){s<<"{";bool a=1;for(auto e:m){s<<(a?"":" ")<<e.first<<":"<<e.second;a=0;}s<<"}";return s;}
#define DUMP(x)  cerr<<#x<<" = "<<(x)<<endl;

struct edge { int to, cost; };

const int INF = 1e18;
const int MOD = 1e9+7;

signed main()
{
   int H, W, K; cin >> H >> W >> K;
   V<V<char>> A(H, V<char>(W));
   P<int,int> s;
   for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
         cin >> A[i][j];
         if (A[i][j] == 'S') {
            s = make_pair(i, j);
         }
      }
   }

   int MIN = INF;
   for (int i = 0; s.first + i <= H && i <= K; i++) {
      if (s.first + i == H) {
         chmin(MIN, 0LL);
         break;
      }
      if (A[s.first + i][s.second] == '#' || i == K) {
         chmin(MIN, H - (s.first + i));
         break;
      }
   }
   for (int i = 0; s.first - i >= -1 && i <= K; i++) {
      if (s.first - i == -1) {
         chmin(MIN, 0LL);
         break;
      }
      if (A[s.first - i][s.second] == '#' || i == K) {
         chmin(MIN, (s.first - i) + 1);
         break;
      }
   }
   for (int j = 0; s.second + j <= W && j <= K; j++) {
      if (s.second + j == W) {
         chmin(MIN, 0LL);
         break;
      }
      if (A[s.first][s.second + j] == '#' || j == K) {
         chmin(MIN, W - (s.second + j));
         break;
      }
   }
   for (int j = 0; s.second - j >= -1 && j <= K; j++) {
      if (s.second - j == -1) {
         chmin(MIN, 0LL);
         break;
      }
      if (A[s.first][s.second - j] == '#' || j == K) {
         chmin(MIN, (s.second - j) + 1);
         break;
      }
   }
   cout << (MIN == 0 ? 1 : (MIN-1) / K + 2) << endl;

   return 0;
}
