#include <bits/stdc++.h>
using namespace std;
const int INF = (long long int)(1LL << 31) - 1;
const long long int INFl = (long long int)9223372036854775807;
const int MAX = 10000;
const long long int MOD = (long long int)1e9 + 7;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b) {
  return a / gcd(a, b) * b;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
int h, w, k;
char a[810][810];
pair<int, int> st;
int t[810][810];
queue<pair<int, int> > q;
void bfs() {
  q.push(st);
  while (q.size()) {
    pair<int, int> qq = q.front();
    q.pop();
    if (qq.first > 0 && a[qq.first - 1][qq.second] == '.' &&
        t[qq.first - 1][qq.second] == -1) {
      t[qq.first - 1][qq.second] = t[qq.first][qq.second] + 1;
      if (t[qq.first - 1][qq.second] < k)
        q.push(pair<int, int>(qq.first - 1, qq.second));
    }
    if (qq.first < h - 1 && a[qq.first + 1][qq.second] == '.' &&
        t[qq.first + 1][qq.second] == -1) {
      t[qq.first + 1][qq.second] = t[qq.first][qq.second] + 1;
      if (t[qq.first + 1][qq.second] < k)
        q.push(pair<int, int>(qq.first + 1, qq.second));
    }
    if (qq.second > 0 && a[qq.first][qq.second - 1] == '.' &&
        t[qq.first][qq.second - 1] == -1) {
      t[qq.first][qq.second - 1] = t[qq.first][qq.second] + 1;
      if (t[qq.first][qq.second - 1] < k)
        q.push(pair<int, int>(qq.first, qq.second - 1));
    }
    if (qq.second < w - 1 && a[qq.first][qq.second + 1] == '.' &&
        t[qq.first][qq.second + 1] == -1) {
      t[qq.first][qq.second + 1] = t[qq.first][qq.second] + 1;
      if (t[qq.first][qq.second + 1] < k)
        q.push(pair<int, int>(qq.first, qq.second + 1));
    }
  }
}
int main() {
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') st = pair<int, int>(i, j);
    }
  for (int i = 0; i < 810; i++) {
    for (int j = 0; j < 810; j++) {
      t[i][j] = -1;
    }
  }
  t[st.first][st.second] = 0;
  bfs();
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i == 0 && t[i][j] != -1) {
        cout << 1 << endl;
        return 0;
      }
      if (j == 0 && t[i][j] != -1) {
        cout << 1 << endl;
        return 0;
      }
      if (i == h - 1 && t[i][j] != -1) {
        cout << 1 << endl;
        return 0;
      }
      if (j == w - 1 && t[i][j] != -1) {
        cout << 1 << endl;
        return 0;
      }
    }
  }
  long long int ans = INFl;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (t[i][j] != -1) {
        ans = ((ans) < ((((((i + k - 1) / k) < ((h - i + k - 1) / k)
                               ? ((i + k - 1) / k)
                               : ((h - i + k - 1) / k))) <
                                 ((((j + k - 1) / k) < ((w - j + k - 1) / k)
                                       ? ((j + k - 1) / k)
                                       : ((w - j + k - 1) / k)))
                             ? ((((i + k - 1) / k) < ((h - i + k - 1) / k)
                                     ? ((i + k - 1) / k)
                                     : ((h - i + k - 1) / k)))
                             : ((((j + k - 1) / k) < ((w - j + k - 1) / k)
                                     ? ((j + k - 1) / k)
                                     : ((w - j + k - 1) / k)))))
                   ? (ans)
                   : ((((((i + k - 1) / k) < ((h - i + k - 1) / k)
                             ? ((i + k - 1) / k)
                             : ((h - i + k - 1) / k))) <
                               ((((j + k - 1) / k) < ((w - j + k - 1) / k)
                                     ? ((j + k - 1) / k)
                                     : ((w - j + k - 1) / k)))
                           ? ((((i + k - 1) / k) < ((h - i + k - 1) / k)
                                   ? ((i + k - 1) / k)
                                   : ((h - i + k - 1) / k)))
                           : ((((j + k - 1) / k) < ((w - j + k - 1) / k)
                                   ? ((j + k - 1) / k)
                                   : ((w - j + k - 1) / k))))));
      }
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
