#include <bits/stdc++.h>
using namespace std;
template <typename T0, typename T1>
std::ostream& operator<<(std::ostream& os, const map<T0, T1>& v) {
  for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) {
    os << p->first << ": " << p->second << " ";
  }
  return os;
}
template <typename T0, typename T1>
std::ostream& operator<<(std::ostream& os, const pair<T0, T1>& v) {
  os << v.first << ": " << v.second << " ";
  return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const vector<T>& v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << " ";
  }
  return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const vector<vector<T> >& v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << endl;
  }
  return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const set<T>& v) {
  vector<T> tmp(v.begin(), v.end());
  os << tmp;
  return os;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, const deque<T>& v) {
  vector<T> tmp(v.begin(), v.end());
  os << tmp;
  return os;
}
int main() {
  long long W, H, K;
  string s;
  while (cin >> H >> W >> K) {
    vector<string> w(H);
    deque<pair<long long, long long> > q;
    vector<vector<long long> > p(H, vector<long long>(W, (1LL << 60)));
    for (int i = 0, _n = (H); (i) < (int)_n; ++i) {
      cin >> w[i];
      for (int j = 0, _n = (W); (j) < (int)_n; ++j)
        if (w[i][j] == 'S') {
          q.push_back(make_pair(j, i));
          p[j][i] = 0;
        }
    }
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    while (q.size()) {
      pair<long long, long long> c = q.front();
      q.pop_front();
      long long x = c.first;
      long long y = c.second;
      for (int d = 0, _n = (4); (d) < (int)_n; ++d) {
        long long nx = x + dx[d];
        long long ny = y + dy[d];
        long long ncost = p[y][x] + 1;
        if (((0) <= (nx) && (nx) < (W)) && ((0) <= (ny) && (ny) < (H)) &&
            w[ny][nx] == '.' && p[ny][nx] > ncost) {
          p[ny][nx] = ncost;
          q.push_back(make_pair(nx, ny));
        }
      }
    }
    long long ans = (1LL << 60);
    for (int y = 0, _n = (H); (y) < (int)_n; ++y)
      for (int x = 0, _n = (W); (x) < (int)_n; ++x)
        if (p[y][x] <= K) {
          ans = min(ans, 1 + (x + K - 1) / K);
          ans = min(ans, 1 + (W - 1 - x + K - 1) / K);
          ans = min(ans, 1 + (y + K - 1) / K);
          ans = min(ans, 1 + (H - 1 - y + K - 1) / K);
        }
    cout << ans << endl;
  }
  return 0;
}
