#include <bits/stdc++.h>
using namespace std;
const int OO = 1e9;
const long long INF = 1e18;
const int irand(int lo, int hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}
const long long lrand(long long lo, long long hi) {
  return ((double)rand() / (RAND_MAX + 1.0)) * (hi - lo + 1) + lo;
}
template <typename T>
T getnum() {
  int sign = 1;
  T ret = 0;
  char c;
  do {
    c = getchar();
  } while (c == ' ' || c == '\n');
  if (c == '-')
    sign = -1;
  else
    ret = c - '0';
  while (1) {
    c = getchar();
    if (c < '0' || c > '9') break;
    ret = 10 * ret + c - '0';
  }
  return sign * ret;
}
inline void ini(int& x) { x = getnum<int>(); }
inline void scani(int& x) { scanf("%d", &x); }
const int cx[] = {0, 1, 0, -1};
const int cy[] = {1, 0, -1, 0};
const int N = 805;
int n, m, k;
char grid[N][N];
queue<pair<int, int> > q;
bool vis[N][N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  cout.precision(10);
  cin >> n >> m >> k;
  int sx, sy;
  for (int i = 1; i <= n; i += 1) {
    for (int j = 1; j <= m; j += 1) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  vis[sx][sy] = true;
  q.push(make_pair(sx, sy));
  int ans = -1;
  for (int step = 0; !q.empty() && step < k; step++) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int d = 0; d < 4; d++) {
      int tx = x + cx[d];
      int ty = y + cy[d];
      if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
      if (grid[tx][ty] == '#') continue;
      if (vis[tx][ty]) continue;
      vis[tx][ty] = true;
      q.push(make_pair(tx, ty));
      if (tx == 1 || tx == n || ty == 1 || ty == m) {
        ans = 0;
      }
    }
  }
  while (!q.empty()) q.pop();
  if (ans == -1) {
    for (int i = 1; i <= n; i += 1) {
      for (int j = 1; j <= m; j += 1) {
        if (vis[i][j]) {
          q.push(make_pair(i, j));
        }
      }
    }
    for (int step = 0; !q.empty(); step++) {
      int cast = step / k + 1;
      for (int sz = q.size(); sz > 0; sz--) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        for (int d = 0; d < 4; d++) {
          int tx = x + cx[d];
          int ty = y + cy[d];
          if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
          if (vis[tx][ty]) continue;
          vis[tx][ty] = true;
          q.push(make_pair(tx, ty));
          if (tx == 1 || tx == n || ty == 1 || ty == m) {
            ans = cast;
            goto finish;
          }
        }
      }
    }
  finish:;
  }
  cout << ans + 1 << '\n';
  return 0;
}
