#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << " ";
    os << *it;
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto x : mp) os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template <typename T, long long SIZE>
long long array_length(const T (&)[SIZE]) {
  return SIZE;
}
template <typename T, long long N>
void PRINTF(const T (&a)[N], long long s = N, long long t = -1, bool f = true) {
  if (t == -1) {
    for (long long i = 0; i < s; i++) {
      if (i) cout << " ";
      cout << a[i];
    }
  } else
    for (long long i = s; i < t; i++) {
      if (i != s) cout << " ";
      cout << a[i];
    }
  if (f) cout << "\n";
}
template <typename T, long long N1, long long N2>
void PRINTF(const T (&a)[N1][N2], long long h = N1, long long w = N2) {
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cout << a[i][j] << " \n"[j == w - 1];
    }
  }
}
const long long inf = 1e9 + 7;
const long long INF = 1e18 + 7;
string str[1000];
bool used[1000 * 1000];
long long dy[] = {-1, 0, 0, 1}, dx[] = {0, -1, 1, 0};
long long h, w, k;
bool ok(long long y, long long x) {
  if (y == 0 or y == h - 1) return false;
  if (x == 0 or x == w - 1) return false;
  return true;
}
long long distance(pair<long long, long long> p) {
  long long y = p.first, x = p.second;
  long long yy = min(y, (h - 1) - y);
  long long xx = min(x, (w - 1) - x);
  return min(yy, xx);
}
signed main() {
  scanf("%lld%lld%lld", &h, &w, &k);
  for (long long i = 0; i < h; i++) {
    cin >> str[i];
  }
  pair<long long, long long> s;
  for (long long i = 0; i < h; i++)
    for (long long j = 0; j < w; j++)
      if (str[i][j] == 'S') s = pair<long long, long long>(i, j);
  queue<pair<long long, pair<long long, long long> > > q;
  q.push(pair<long long, pair<long long, long long> >(0, s));
  used[s.first * w + s.second] = true;
  for (long long i = 0; i < h + w; i++) {
    long long ans = 0;
    while (!q.empty()) {
      if (q.front().first == k) break;
      pair<long long, pair<long long, long long> > p = q.front();
      q.pop();
      if (ok(p.second.first, p.second.second) == false) break;
      for (long long j = 0; j < 4; j++) {
        long long yy = p.second.first + dy[j], xx = p.second.second + dx[j];
        if (str[yy][xx] == '#' and i == 0 or used[yy * w + xx]) continue;
        if (ok(yy, xx) == false) ans = 1;
        q.push(pair<long long, pair<long long, long long> >(
            p.first + 1, pair<long long, long long>(yy, xx)));
        used[yy * w + xx] = true;
      }
    }
    if (ans) {
      printf("%lld\n", i + 1);
      return 0;
    }
    q.push(pair<long long, pair<long long, long long> >(0, s));
    long long mn = INF;
    while (!q.empty()) {
      pair<long long, pair<long long, long long> > p = q.front();
      q.pop();
      mn = min(mn, distance(p.second));
    }
    printf("%lld\n", 1 + (mn + k - 1) / k);
    break;
  }
  return 0;
}
