#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const long long linf = 1LL << 62;
const int MAX = 510000;
long long dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
long long dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2>
inline void print2(T1 a, T2 b) {
  cout << a << " " << b << "\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << a << " " << b << " " << c << "\n";
}
const int mod = 1e9 + 7;
bool in(long long y, long long x, long long h, long long w) {
  return 0 <= y && y < h && 0 <= x && x < w;
}
int main() {
  long long h, w, k;
  cin >> h >> w >> k;
  vector<string> s(h);
  for (long long i = 0; i < (h); i++) cin >> s[i];
  vector<vector<long long>> d(h, vector<long long>(w, inf));
  queue<pair<pair<long long, long long>, long long>> q, que;
  for (long long i = 0; i < (h); i++) {
    for (long long j = 0; j < (w); j++) {
      if (s[i][j] == 'S') {
        q.emplace(pair<long long, long long>(i, j), k);
        que.emplace(pair<long long, long long>(i, j), 0);
        d[i][j] = 1;
      }
    }
  }
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    if (p.second == 0) continue;
    long long y = p.first.first;
    long long x = p.first.second;
    for (long long i = 0; i < (4); i++) {
      long long ny = y + dy[i];
      long long nx = x + dx[i];
      if (in(ny, nx, h, w)) {
        if (s[ny][nx] == '#') continue;
        if (d[ny][nx] == 1) continue;
        d[ny][nx] = 1;
        q.emplace(pair<long long, long long>(ny, nx), p.second - 1);
        que.emplace(pair<long long, long long>(ny, nx), 0);
      }
    }
  }
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    long long y = p.first.first;
    long long x = p.first.second;
    for (long long i = 0; i < (4); i++) {
      long long ny = y + dy[i];
      long long nx = x + dx[i];
      if (in(ny, nx, h, w)) {
        if (d[ny][nx] != inf) continue;
        long long c = 0, K;
        if (p.second == 0) {
          c++;
          K = k - 1;
        } else {
          K = p.second - 1;
        }
        d[ny][nx] = d[y][x] + c;
        que.emplace(pair<long long, long long>(ny, nx), K);
      }
    }
  }
  long long ans = inf;
  for (long long i = 0; i < (h); i++) {
    chmin(ans, min(d[i][0], d[i][w - 1]));
  }
  cout << ans << endl;
}
