#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
const ll inf = (1e18);
const ll mod = 1000000007;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(4);
  }
} __init;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
V<int> dy = {-1, 0, 1, 0}, dx = {0, -1, 0, 1};
struct data {
  int y, x, b, cnt;
  data(int y, int x, int b, int cnt) : y(y), x(x), b(b), cnt(cnt) {}
};
bool ok[805][805];
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  V<string> s(h);
  for (int i = 0; i < h; i++) cin >> s[i];
  V<V<int>> d(h, V<int>(w, 1e6));
  for (int i = 0; i < h; i++) {
    ok[i][0] = 1;
    ok[i][w - 1] = 1;
  }
  for (int i = 0; i < w; i++) {
    ok[0][i] = 1;
    ok[h - 1][i] = 1;
  }
  queue<data> q;
  queue<data> qq;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == 'S') {
        d[i][j] = 1;
        q.emplace(i, j, 0, 0);
      }
    }
  }
  int times = 1;
  while (q.size()) {
    int yy = q.front().y, xx = q.front().x, bb = q.front().b, c = q.front().cnt;
    if (d[yy][xx] == times) qq.emplace(yy, xx, bb + k, 0);
    q.pop();
    for (int i = 0; i < 4; i++) {
      int ny = dy[i] + yy, nx = dx[i] + xx;
      if (s[ny][nx] == '#') continue;
      if (ok[ny][nx]) {
        cout << 1 << "\n";
        return 0;
      }
      if (d[ny][nx] > d[yy][xx]) {
        d[ny][nx] = d[yy][xx];
        if (c + 1 > k) continue;
        q.emplace(ny, nx, bb, c + 1);
      }
    }
  }
  while (1) {
    while (qq.size()) {
      q.emplace(qq.front());
      qq.pop();
    }
    times++;
    while (q.size()) {
      int yy = q.front().y, xx = q.front().x, bb = q.front().b,
          c = q.front().cnt;
      if (d[yy][xx] == times) qq.emplace(yy, xx, bb + k, 0);
      q.pop();
      for (int i = 0; i < 4; i++) {
        int ny = dy[i] + yy, nx = dx[i] + xx;
        if (bb == 0 && s[ny][nx] == '#') continue;
        if (ok[ny][nx]) {
          cout << times << "\n";
          return 0;
        }
        if (d[ny][nx] > times) {
          d[ny][nx] = times;
          if (c + 1 > k) continue;
          if (s[ny][nx] == '#')
            q.emplace(ny, nx, bb - 1, c + 1);
          else
            q.emplace(ny, nx, bb, c + 1);
        }
      }
    }
  }
}
