#include <bits/stdc++.h>
using std::abs;
using std::cerr;
using std::cin;
using std::cout;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::reverse;
using std::set;
using std::sort;
using std::string;
using std::swap;
using std::vector;
using ll = long long;
class solver {
 private:
  double TIME;
  static const ll INF = static_cast<ll>(1e9) + 7;
  static const int MAXN = static_cast<ll>(1e6) + 17;
  const int MAGIC = 62;
  int n, m, k;
  vector<string> s;

 public:
  static bool FLAG;
  solver() { TIME = static_cast<double>(clock()) / CLOCKS_PER_SEC; }
  bool read() {
    if (!(cin >> n >> m >> k)) return false;
    s.resize(n);
    for (ll i = 0; i < (ll)n; ++i) cin >> s[i];
    return true;
  }
  bool valid(int i, int j) { return 0 <= i && i < n && 0 <= j && j < m; }
  void solve() {
    vector<pair<int, int> > dd = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    int I, J;
    for (ll i = 0; i < (ll)n; ++i)
      for (ll j = 0; j < (ll)m; ++j)
        if (s[i][j] == 'S') I = i, J = j;
    int inf = INF;
    vector<vector<int> > D(n, vector<int>(m, inf));
    std::queue<pair<int, int> > Q;
    D[I][J] = 0;
    Q.push(std::make_pair(I, J));
    while (!Q.empty()) {
      I = Q.front().first;
      J = Q.front().second;
      Q.pop();
      for (auto d : dd)
        if (valid(I + d.first, J + d.second)) {
          int i = I + d.first;
          int j = J + d.second;
          if (D[i][j] != inf || s[i][j] == '#') continue;
          D[i][j] = D[I][J] + 1;
          Q.push(std::make_pair(i, j));
        }
    }
    int ans = inf;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        if (D[i][j] != inf) {
          int t = min({i, j, n - i - 1, m - j - 1});
          if (D[i][j] < k)
            ans = min(ans, 1 + (t + k - 1) / k);
          else
            ans = min(ans, 1 + (D[i][j] + t - 1) / k);
        }
    cout << ans << '\n';
  }
  ~solver() {}
};
bool solver::FLAG = true;
int main() {
  cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout << std::fixed << std::setprecision(10);
  cerr << std::fixed << std::setprecision(5);
  std::mt19937 rand(time(NULL));
  while (solver::FLAG) {
    solver solution;
    if (!solution.read())
      solver::FLAG = false;
    else
      solution.solve();
  }
  return 0;
}
