#include <bits/stdc++.h>
using namespace std;
const long long N = (800 + 5);
const long long MOD = 1e9 + 7;
const long long INF = LLONG_MAX;
const long long LOG = 29;
long long binpow(long long a, long long b) {
  a %= MOD;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  res %= MOD;
  return res;
}
long long n, m, k;
char a[N][N];
long long dis[N][N];
long long w[N][N];
void reset() {
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= m; j++) dis[i][j] = INF;
}
bool vibing(long long i, long long j) {
  if (i < 1 || i > n || j < 1 || j > m) return false;
  return true;
}
void solve() {
  cin >> n >> m >> k;
  long long x, y;
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') x = i, y = j;
      if (a[i][j] == '#') w[i][j] = 1;
    }
  }
  reset();
  dis[x][y] = 0;
  deque<pair<long long, long long> > q;
  q.push_front(make_pair(x, y));
  while (!q.empty()) {
    pair<long long, long long> p = q.front();
    q.pop_front();
    long long dx, dy;
    dx = p.first, dy = p.second;
    if (vibing(dx + 1, dy) && dis[dx][dy] + w[dx + 1][dy] < dis[dx + 1][dy]) {
      dis[dx + 1][dy] = dis[dx][dy] + w[dx + 1][dy];
      if (w[dx + 1][dy] == 1)
        q.push_back(make_pair(dx + 1, dy));
      else
        q.push_front(make_pair(dx + 1, dy));
    }
    dx = p.first, dy = p.second;
    if (vibing(dx - 1, dy) && dis[dx][dy] + w[dx - 1][dy] < dis[dx - 1][dy]) {
      dis[dx - 1][dy] = dis[dx][dy] + w[dx - 1][dy];
      if (w[dx - 1][dy] == 1)
        q.push_back(make_pair(dx - 1, dy));
      else
        q.push_front(make_pair(dx - 1, dy));
    }
    dx = p.first, dy = p.second;
    if (vibing(dx, dy + 1) && dis[dx][dy] + w[dx][dy + 1] < dis[dx][dy + 1]) {
      dis[dx][dy + 1] = dis[dx][dy] + w[dx][dy + 1];
      if (w[dx][dy + 1] == 1)
        q.push_back(make_pair(dx, dy + 1));
      else
        q.push_front(make_pair(dx, dy + 1));
    }
    dx = p.first, dy = p.second;
    if (vibing(dx, dy - 1) && dis[dx][dy] + w[dx][dy - 1] < dis[dx][dy - 1]) {
      dis[dx][dy - 1] = dis[dx][dy] + w[dx][dy - 1];
      if (w[dx][dy - 1] == 1)
        q.push_back(make_pair(dx, dy - 1));
      else
        q.push_front(make_pair(dx, dy - 1));
    }
  }
  long long ans = INF;
  for (long long i = 1; i <= m; i++) {
    long long cast = dis[1][i];
    long long gg2 = 0;
    if (cast % k != 0) gg2 = 1;
    cast /= k;
    cast += gg2;
    long long cast2 = abs(x - 1) + abs(y - i);
    gg2 = 0;
    if (cast2 % k != 0) gg2 = 1;
    cast2 /= k;
    cast2 += gg2;
    if (cast2 > cast) {
      ans = min(ans, cast2);
    } else
      ans = min(ans, cast + 1);
  }
  for (long long i = 1; i <= m; i++) {
    long long cast = dis[n][i];
    long long gg2 = 0;
    if (cast % k != 0) gg2 = 1;
    cast /= k;
    cast += gg2;
    long long cast2 = abs(x - n) + abs(y - i);
    gg2 = 0;
    if (cast2 % k != 0) gg2 = 1;
    cast2 /= k;
    cast2 += gg2;
    if (cast2 > cast) {
      ans = min(ans, cast2);
    } else
      ans = min(ans, cast + 1);
  }
  for (long long i = 1; i <= n; i++) {
    long long cast = dis[i][1];
    long long gg2 = 0;
    if (cast % k != 0) gg2 = 1;
    cast /= k;
    cast += gg2;
    long long cast2 = abs(x - i) + abs(y - 1);
    gg2 = 0;
    if (cast2 % k != 0) gg2 = 1;
    cast2 /= k;
    cast2 += gg2;
    if (cast2 > cast) {
      ans = min(ans, cast2);
    } else
      ans = min(ans, cast + 1);
  }
  for (long long i = 1; i <= n; i++) {
    long long cast = dis[i][n];
    long long gg2 = 0;
    if (cast % k != 0) gg2 = 1;
    cast /= k;
    cast += gg2;
    long long cast2 = abs(x - i) + abs(y - n);
    gg2 = 0;
    if (cast2 % k != 0) gg2 = 1;
    cast2 /= k;
    cast2 += gg2;
    if (cast2 > cast) {
      ans = min(ans, cast2);
    } else
      ans = min(ans, cast + 1);
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
}
