#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template <typename T>
using VT = vector<T>;
template <typename T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
const double EPS = 1e-9;
const int dh[] = {0, -1, 0, 1};
const int dw[] = {1, 0, -1, 0};
const int MAX = 888;
int H, W, K;
string A[MAX];
char strbuf[MAX];
int vis[MAX][MAX];
string next() {
  scanf("%s", strbuf);
  return strbuf;
}
bool inside(int h, int w) { return 0 <= h && h < H && 0 <= w && w < W; }
bool inside(PII p) { return inside(p.first, p.second); }
bool isGoal(int h, int w) {
  return h == 0 || w == 0 || h == H - 1 || w == W - 1;
}
bool isGoal(PII p) { return isGoal(p.first, p.second); }
int main() {
  memset(vis, -1, sizeof(vis));
  scanf("%d%d%d", &H, &W, &K);
  PII start;
  for (int(i) = 0; (i) < (H); (i)++) {
    A[i] = next();
    for (int(j) = 0; (j) < (W); (j)++)
      if (A[i][j] == 'S') {
        start = make_pair(i, j);
      }
  }
  queue<pair<int, PII>> q;
  q.push(make_pair(K, start));
  while (!q.empty()) {
    auto el = q.front();
    q.pop();
    if (isGoal(el.second)) {
      cout << 1 << endl;
      return 0;
    }
    if (!chmax(vis[el.second.first][el.second.second], el.first)) {
      continue;
    }
    if (el.first)
      for (int(i) = 0; (i) < (4); (i)++) {
        PII next = make_pair(el.second.first + dh[i], el.second.second + dw[i]);
        if (!inside(next)) {
          continue;
        }
        if (A[next.first][next.second] == '#') {
          continue;
        }
        q.push(make_pair(el.first - 1, next));
      }
  }
  int ans = INF;
  for (int(i) = 0; (i) < (H); (i)++) {
    for (int(j) = 0; (j) < (W); (j)++) {
      if (vis[i][j] != -1) {
        chmin(ans, i / K + (i % K != 0) + 1);
        chmin(ans, j / K + (j % K != 0) + 1);
        chmin(ans, (H - i) / K + ((H - i) % K != 0) + 1);
        chmin(ans, (W - j) / K + ((W - j) % K != 0) + 1);
      }
    }
  }
  cout << ans << endl;
}
