#include <bits/stdc++.h>
using namespace std;
long long h, w, k;
bool cando[805][805], block[805][805];
long long dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
void dfs(long long x, long long y, long long d) {
  if (d >= k) return;
  for (long long i = 0; i < 4; i++) {
    long long nx = x + dx[i], ny = y + dy[i];
    if (0 < nx && nx <= h && 0 < ny && ny <= w) {
      if (!cando[nx][ny] && !block[nx][ny]) {
        cando[nx][ny] = true;
        dfs(nx, ny, d + 1);
      }
    }
  }
}
long long dis(long long x, long long y) {
  long long d1 = x - 1, d2 = h - x, d3 = y - 1, d4 = w - y;
  return min({d1, d2, d3, d4});
}
signed main() {
  cin >> h >> w >> k;
  long long sx, sy;
  for (long long i = 1; i <= h; i++) {
    string s;
    cin >> s;
    for (long long j = 1; j <= w; j++) {
      if (s[j - 1] == 'S') {
        cando[i][j] = true;
        sx = i;
        sy = j;
      } else
        cando[i][j] = false;
      if (s[j - 1] == '#')
        block[i][j] = true;
      else
        block[i][j] = false;
    }
  }
  if (sx == 1 || sx == h || sy == 1 || sy == w) {
    cout << 0 << endl;
    return 0;
  }
  dfs(sx, sy, 0);
  long long mdis = 174417441744;
  for (long long i = 1; i <= h; i++) {
    for (long long j = 1; j <= w; j++) {
      if (cando[i][j]) mdis = min(mdis, dis(i, j));
    }
  }
  long long ans = mdis / k + (mdis % k == 0 ? 0 : 1);
  if (ans == 0) {
    while (1) cout << "E" << endl;
  } else
    cout << ans + 1 << endl;
}
