#include <bits/stdc++.h>
using namespace std;
template <class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
  ost << "{" << p.first << ", " << p.second << "} ";
  return ost;
}
template <class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
  ost << "{";
  for (int i = 0; i < (int)v.size(); i++) {
    if (i) ost << " ";
    ost << v[i];
  }
  ost << "} \n";
  return ost;
}
template <class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>& v) {
  ost << "{";
  for (auto p : v) {
    ost << "{" << p.first << ", " << p.second << "} ";
  }
  ost << "} ";
  return ost;
}
void out() { cout << "\n"; }
template <class T, class... Args>
void out(const T& x, const Args&... args) {
  cout << x << " ";
  out(args...);
}
template <typename T>
istream& operator>>(istream& is, vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}
template <typename T>
string join(vector<T>& vec, string sep = " ") {
  stringstream ss;
  for (int i = 0; i < (int)vec.size(); i++) {
    ss << vec[i] << (i + 1 == (int)vec.size() ? "\n" : sep);
  }
  return ss.str();
}
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 scan(int& a) { cin >> a; }
void scan(long long& a) { cin >> a; }
void scan(char& a) { cin >> a; }
void scan(double& a) { cin >> a; }
void scan(string& a) { cin >> a; }
void scan(long double& a) { cin >> a; }
void IN() {}
template <class Head, class... Tail>
void IN(Head& head, Tail&... tail) {
  scan(head);
  IN(tail...);
}
void YES(bool b) { cout << ((b) ? "YES\n" : "NO\n"); }
void Yes(bool b) { cout << ((b) ? "Yes\n" : "No\n"); }
void yes(bool b) { cout << ((b) ? "yes\n" : "no\n"); }
void Yay(bool b) { cout << ((b) ? "Yay!\n" : ":(\n"); }
const long double pi = acos(-1.0);
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return c;
}
long long nCrmod(long long n, long long r) {
  long long x = 1, y = 1;
  for (long long i = 0; i < r; i++) {
    x = x * (n - i) % mod;
    y = y * (i + 1) % mod;
  }
  return x * powmod(y, mod - 2) % mod;
}
long long nHrmod(long long n, long long r) {
  return (nCrmod(n + r - 1, r) % mod);
}
long long gcd(long long a, long long b) {
  while (b) {
    long long c = b;
    b = a % b;
    a = c;
  }
  return a;
}
long long lcm(long long a, long long b) {
  if (!a || !b) return 0;
  return a * b / gcd(a, b);
}
vector<long long> divisor(long long x) {
  vector<long long> v;
  for (int i = 1; i * i <= x; i++)
    if (x % i == 0) {
      v.push_back(i);
      if (i * i != x) v.push_back(x / i);
    }
  sort((v).begin(), (v).end());
  return v;
};
map<long long, long long> prime_factor(long long n) {
  map<long long, long long> m;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      m[i]++;
      n /= i;
    }
  }
  if (n != 1) m[n] = 1;
  return m;
}
long long dy[] = {-1, 0, 0, 1};
long long dx[] = {0, -1, 1, 0};
int main() {
  long long h, w, k;
  IN(h, w, k);
  long long sy, sx;
  vector<vector<long long>> a(h, vector<long long>(w, -1));
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      char c;
      IN(c);
      if (c == 'S') sy = i, sx = j;
      if (c == '#') a[i][j] = -2;
    }
  }
  vector<pair<long long, long long>> memo;
  queue<pair<long long, long long>> q;
  q.push({sy, sx});
  memo.push_back({sy, sx});
  a[sy][sx] = 0;
  while (!q.empty()) {
    long long y = q.front().first;
    long long x = q.front().second;
    q.pop();
    for (long long i = 0; i < 4; i++) {
      long long ny = y + dy[i];
      long long nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
      if (a[ny][nx] == -1) {
        a[ny][nx] = a[y][x] + 1;
        memo.push_back({ny, nx});
        if (a[y][x] + 1 <= k) {
          q.push({ny, nx});
        }
      }
    }
  }
  long long ans = ((1LL << 62) - (1LL << 31));
  for (auto e : memo) {
    long long d = min({e.first, e.second, h - 1 - e.first, w - 1 - e.second});
    chmin(ans, (d + k - 1) / k + 1);
  }
  cout << ans << endl;
  return 0;
}
