#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
class modint {
  int v;

 public:
  modint() : v(0) {}
  modint(long long v) : v((v + MOD) % MOD) {}
  bool operator==(modint x) { return v == x.v; }
  bool operator!=(modint x) { return v != x.v; }
  modint operator+(modint x) { return v + x.v; }
  modint operator-(modint x) { return v - x.v; }
  modint operator*(modint x) { return (long long)v * x.v; }
  modint& operator+=(const modint x) { return *this = (*this + x); }
  modint& operator-=(const modint x) { return *this = (*this - x); }
  modint& operator*=(const modint x) { return *this = (*this * x); }
  int operator*() { return v; }
};
int H, W, K;
char D[805][805];
int si, sj;
int go(int price) {
  static int dst[805][805];
  static int moves[805][805];
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int> >,
                 greater<tuple<int, int, int> > >
      pq;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++) dst[i][j] = moves[i][j] = 1e9;
  dst[si][sj] = 0;
  moves[si][sj] = 0;
  pq.push(make_tuple(0, si, sj));
  int rep = 0;
  while (!pq.empty()) {
    int c, x, y;
    tie(c, x, y) = pq.top();
    pq.pop();
    rep += 1;
    if (dst[x][y] != c) continue;
    if (x == 1 || y == 1 || x == H || y == W) {
      assert(((dst[x][y] - moves[x][y]) / price) * price + moves[x][y] ==
             dst[x][y]);
      int a = (dst[x][y] - moves[x][y]) / price;
      int b = moves[x][y];
      return max((a + K - 1) / K, (b + K - 1) / K);
    }
    for (pair<int, int> d : {make_pair(-1, 0), make_pair(1, 0),
                             make_pair(0, -1), make_pair(0, 1)}) {
      int nx = x + d.first;
      int ny = y + d.second;
      int nc = c + ((D[nx][ny] == '#' ? price : 0) + 1);
      if (1 <= nx && nx <= H && 1 <= ny && ny <= W && dst[nx][ny] > nc) {
        dst[nx][ny] = nc;
        moves[nx][ny] = moves[x][y] + 1;
        pq.push(make_tuple(nc, nx, ny));
      }
    }
  }
  return 1e9;
}
int ans = 1e9;
int main() {
  scanf("%d%d%d", &H, &W, &K);
  for (int i = 1; i <= H; i++) {
    scanf("%s", D[i] + 1);
    for (int j = 1; j <= W; j++) {
      if (D[i][j] == 'S') {
        si = i;
        sj = j;
        D[i][j] = '.';
      }
    }
  }
  int q = 100000;
  for (int i = 1; i <= 30; i++) {
    int p = go(i);
    if (p > q) break;
    q = p;
    ans = min(ans, p);
  }
  printf("%d\n", ans);
  return 0;
}
