#include <bits/stdc++.h>
using namespace std;
const int N = 810;
char s[N][N];
int vis[N][N];
int n, m, K, sx, sy;
bool ok(int x, int y) {
  if (x == 0 || y == 0 || x == n - 1 || y == m - 1) return 1;
  return 0;
}
int xl[4] = {-1, 1, 0, 0};
int yl[4] = {0, 0, -1, 1};
int solve() {
  vis[sx][sy] = 0;
  queue<pair<int, int> > Q;
  Q.push(make_pair(sx, sy));
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    if (vis[p.first][p.second] == K) {
      continue;
    }
    if (ok(p.first, p.second)) {
      return 1;
    }
    for (int i = 0; i < 4; i++) {
      int a = p.first + xl[i];
      int b = p.second + yl[i];
      if (a < 0 || a >= n || b < 0 || b >= m) continue;
      if (s[a][b] == '#') continue;
      vis[a][b] = vis[p.first][p.second] + 1;
      Q.push(make_pair(a, b));
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (vis[i][j] != -1) {
        vis[i][j] = K;
        Q.push(make_pair(i, j));
      }
    }
  }
  while (!Q.empty()) {
    pair<int, int> p = Q.front();
    Q.pop();
    if (ok(p.first, p.second)) {
      int ans = vis[p.first][p.second];
      if (ans % K == 0) {
        return ans / K;
      } else {
        return ans / K + 1;
      }
    }
    for (int i = 0; i < 4; i++) {
      int a = p.first + xl[i];
      int b = p.second + yl[i];
      if (a < 0 || a >= n || b < 0 || b >= m) continue;
      vis[a][b] = vis[p.first][p.second] + 1;
      Q.push(make_pair(a, b));
    }
  }
}
int main() {
  while (~scanf("%d %d %d", &n, &m, &K)) {
    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
      for (int j = 0; j < m; j++) {
        if (s[i][j] == 'S') {
          sx = i;
          sy = j;
          s[i][j] = '.';
        }
        vis[i][j] = -1;
      }
    }
    printf("%d\n", solve());
  }
  return 0;
}
