#include <bits/stdc++.h>
using namespace std;
struct Double {
  double d;
  explicit Double(double x) : d(x) {}
};
ostream& operator<<(ostream& os, const Double x) {
  os << fixed << setprecision(20) << x.d;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  os << "[";
  for (const auto& v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
constexpr double eps = 1e-14;
constexpr long long mod = 1e9 + 7;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int h, w, k;
int sx, sy;
vector<pair<int, int> > stat;
int go(const vector<string>& maze, int gx, int gy) {
  int ret = 1 << 29;
  for (auto st : stat) {
    int x = st.first;
    int y = st.second;
    int cnt = 0;
    int gdist = abs(gx - x) + abs(gy - y);
    cnt += gdist / k;
    if (gdist % k > 0) cnt++;
    ret = min(ret, cnt);
  }
  return ret;
}
int main() {
  cin >> h >> w >> k;
  vector<string> s(h);
  for (int i = 0; i < h; i++) cin >> s[i];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
        break;
      }
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (abs(i - sx) + abs(j - sy) <= k && s[i][j] == '.') {
        s[i][j] = 'S';
      }
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (s[i][j] == 'S') stat.push_back(pair<int, int>(i, j));
  int ans = 1 << 29;
  for (int i = 0; i < h; i++) ans = min(ans, go(s, i, 0));
  for (int i = 0; i < h; i++) ans = min(ans, go(s, i, w - 1));
  for (int i = 0; i < w; i++) ans = min(ans, go(s, 0, i));
  for (int i = 0; i < w; i++) ans = min(ans, go(s, h - 1, i));
  cout << ans + 1 << endl;
}
