#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
void Main() {
  int H, W, K;
  cin >> H >> W >> K;
  char a[H][W];
  for (int i = (0); i < (H); ++i)
    for (int j = (0); j < (W); ++j) cin >> a[i][j];
  int sx, sy;
  for (int i = (0); i < (H); ++i)
    for (int j = (0); j < (W); ++j)
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  int d[H][W];
  for (int i = (0); i < (H); ++i)
    for (int j = (0); j < (W); ++j) d[i][j] = INF;
  vector<pair<int, int> > v{pair<int, int>(1, 0), pair<int, int>(-1, 0),
                            pair<int, int>(0, 1), pair<int, int>(0, -1)};
  int ans = INF;
  queue<tuple<int, int, int> > q;
  q.emplace(sx, sy, 0);
  while (!q.empty()) {
    int i, j, cost;
    tie(i, j, cost) = q.front();
    q.pop();
    if (cost >= K) continue;
    if (a[i][j] == '#' || d[i][j] < INF) continue;
    if (i == 0 || j == 0 || i == H - 1 || j == W - 1) {
      ans = 1;
      break;
    }
    d[i][j] = cost;
    int mn = (min({i, j, H - 1 - i, W - 1 - j}) + K - 1) / K;
    chmin(ans, 1 + mn);
    for (auto& p : v) {
      int x = i + p.first, y = j + p.second;
      q.emplace(x, y, cost + 1);
    }
  }
  cout << ans << "\n";
  return;
}
int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1;
  for (int _ = (0); _ < (t); ++_) Main();
  return 0;
}
