#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<long long>>;
const long long mod = 1000000007;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x * y / gcd(x, y); }
long long roundup(long long a, long long b) { return (a - 1) / b + 1; }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }
signed main() {
  long long h, w, K;
  cin >> h >> w >> K;
  vector<string> s(h);
  for (long long i = (0); i < (h); ++i) cin >> s[i];
  queue<pair<long long, long long>> que;
  long long sx, sy;
  for (long long i = (0); i < (h); ++i)
    for (long long j = (0); j < (w); ++j)
      if (s[i][j] == 'S') sx = i, sy = j;
  que.push(pair<long long, long long>(sx, sy));
  vector<vector<long long>> d(h, vector<long long>(w, -1));
  d[sx][sy] = 0;
  long long dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, 1, -1};
  while (!que.empty()) {
    pair<long long, long long> p = que.front();
    que.pop();
    for (long long i = (0); i < (4); ++i) {
      long long nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' &&
          d[nx][ny] == -1) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(pair<long long, long long>(nx, ny));
      }
    }
  }
  long long ans = INT_MAX;
  for (long long i = (0); i < (h); ++i) {
    long long tmp = 0;
    for (long long j = (0); j < ((w + 1) / 2); ++j)
      if (s[i][j] != '#') {
        if (d[i][j] == -1)
          tmp++;
        else {
          long long l = j;
          if (l == 0)
            ans = min(ans, (d[i][j] - 1) / K + 1);
          else if (d[i][j] >= K) {
            l = l + d[i][j] - 1;
            ans = min(ans, (l - 1) / K + 1);
          } else {
            ans = min((l - 1 - tmp) / K + 2, ans);
          }
        }
      }
    tmp = 0;
    for (long long j = w - 1; j >= (w + 1) / 2; --j)
      if (s[i][j] != '#') {
        if (d[i][j] == -1)
          tmp++;
        else {
          long long l = w - j - 1;
          if (l == 0)
            ans = min(ans, (d[i][j] - 1) / K + 1);
          else if (d[i][j] >= K) {
            l = l + d[i][j] - 1;
            ans = min(ans, (l - 1) / K + 1);
          } else {
            ans = min((l - 1 - tmp) / K + 2, ans);
          }
        }
      }
  }
  for (long long j = (0); j < (w); ++j) {
    long long tmp = 0;
    for (long long i = (0); i < ((h + 1) / 2); ++i)
      if (s[i][j] != '#') {
        if (d[i][j] == -1)
          tmp++;
        else {
          long long l = i;
          if (l == 0)
            ans = min(ans, (d[i][j] - 1) / K + 1);
          else if (d[i][j] >= K) {
            l = l + d[i][j] - 1;
            ans = min(ans, (l - 1) / K + 1);
          } else {
            ans = min((l - 1 - tmp) / K + 2, ans);
          }
        }
      }
    tmp = 0;
    for (long long i = w - 1; i >= (w + 1) / 2; --i)
      if (s[i][j] != '#') {
        if (d[i][j] == -1)
          tmp++;
        else {
          long long l = h - i - 1;
          if (l == 0)
            ans = min(ans, (d[i][j] - 1) / K + 1);
          else if (d[i][j] >= K) {
            l = l + d[i][j] - 1;
            ans = min(ans, (l - 1) / K + 1);
          } else {
            ans = min((l - 1 - tmp) / K + 2, ans);
          }
        }
      }
  }
  cout << ans << endl;
}
