#include <bits/stdc++.h>
using namespace std;
template <class T>
string tostr(T x) {
  stringstream o;
  o << x;
  return o.str();
}
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T mypow(T x, long long n) {
  T r = 1;
  while (n > 0) {
    if (n & 1) r = r * x;
    x = x * x;
    n >>= 1;
  }
  return r;
}
long long toint(string s) {
  long long v;
  stringstream i(s);
  i >> v;
  return v;
}
bool check(long long x, long long y, long long w, long long h) {
  return x >= 0 && y >= 0 && w > x && h > y;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
const long long dx[8] = {+1, +0, -1, +0, +1, +1, -1, -1};
const long long dy[8] = {+0, -1, +0, +1, -1, +1, -1, +1};
const long long mod = 1e9 + 7;
const long long INF = 1e9;
long long vis[801][801];
signed main() {
  long long h, w, k;
  cin >> h >> w >> k;
  long long sy, sx;
  vector<string> s(h);
  for (long long(y) = (0); y < (h); (y)++) {
    cin >> s[y];
    for (long long(x) = (0); x < (w); (x)++) {
      if (s[y][x] == 'S') {
        sy = y;
        sx = x;
      }
    }
  }
  auto ok = [&](long long m) {
    vector<string> t = s;
    memset((vis), 0, sizeof(vis));
    priority_queue<tuple<long long, long long, long long, long long> > q;
    q.push(tuple<long long, long long, long long, long long>(0, 0, sy, sx));
    for (long long(i) = (0); i < (w); (i)++)
      for (long long(j) = (0); j < (h); (j)++) {
        vis[j][i] = INF;
      }
    vis[sy][sx] = 0;
    while (q.size()) {
      auto now = q.top();
      q.pop();
      long long mv = get<0>(now);
      long long us = get<1>(now);
      long long y = get<2>(now);
      long long x = get<3>(now);
      if (mv > m * k) continue;
      if (mv && mv % k == 0) us += k;
      if (x == 0 || y == 0 || x == w - 1 || y == h - 1) {
        return true;
      }
      for (long long(i) = (0); i < (4); (i)++) {
        long long tx = x + dx[i], ty = y + dy[i];
        if (check(tx, ty, w, h) && vis[ty][tx] > mv + 1) {
          if (s[ty][tx] == '#') {
            if (us) {
              q.push(tuple<long long, long long, long long, long long>(
                  mv + 1, us - 1, ty, tx));
            } else
              continue;
          } else {
            q.push(tuple<long long, long long, long long, long long>(mv + 1, us,
                                                                     ty, tx));
          }
          vis[ty][tx] = mv + 1;
        }
      }
      bool ok = false;
      for (long long(i) = (0); i < (4); (i)++) {
        long long tx = x + dx[i], ty = y + dy[i];
        if (check(tx, ty, w, h) && vis[ty][tx] > (mv / k + 1) * k + 1) {
          ok = true;
        }
      }
      if (ok) {
        q.push(tuple<long long, long long, long long, long long>(
            (mv / k + 1) * k, us, y, x));
      }
    }
    return false;
  };
  long long l = 0, r = INF;
  for (long long(i) = (0); i < (100); (i)++) {
    long long m = (r + l) / 2.0;
    if (ok(m)) {
      r = m;
    } else {
      l = m;
    }
  }
  cout << r << endl;
  return 0;
}
