#include <bits/stdc++.h>
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const long long LINF = 1e18;
using namespace std;
long long n, cnt = 0, ans = LINF, a, b, c, d[1000][1000], cmp, cmpp, m, h, w, x,
             y, sum = 0, pos, k, sy, sx, gx = 1000, gy = 1000, ri, le, up, down;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz");
char s[1000][1000];
bool fl;
struct edge {
  int to, cost;
};
void bfs() {
  queue<pair<int, int>> que;
  for (long long(i) = 0; (i) < (int)(h); (i)++) {
    for (long long(j) = 0; (j) < (int)(w); (j)++) {
      d[i][j] = INF;
    }
  }
  que.push(pair<int, int>(sy, sx));
  d[sy][sx] = 0;
  while ((que).size()) {
    pair<int, int> p = que.front();
    que.pop();
    if (p.first == gy && p.second == gx) break;
    for (long long(i) = 0; (i) < (int)(4); (i)++) {
      int nx = dx[i] + p.second, ny = dy[i] + p.first;
      if (nx >= 0 && nx < w && ny >= 0 && ny < h && s[ny][nx] != '#' &&
          d[ny][nx] == INF) {
        que.push(pair<int, int>(ny, nx));
        d[ny][nx] = d[p.second][p.first] + 1;
      }
    }
  }
  return;
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  for (long long(i) = 0; (i) < (int)(h); (i)++) {
    for (long long(j) = 0; (j) < (int)(w); (j)++) {
      cin >> s[i][j];
      if (s[i][j] == 'S') sy = i, sx = j;
    }
  }
  bfs();
  for (long long(i) = 0; (i) < (int)(h); (i)++) {
    for (long long(j) = 0; (j) < (int)(w); (j)++) {
      if (d[i][j] <= k) {
        if (d[i][j] == 0) continue;
        ri = (w - 1 - j) / k;
        if ((w - 1 - j) % k != 0) ri++;
        le = j / k;
        if (j % k != 0) le++;
        up = i / k;
        if (i % k != 0) up++;
        down = (h - 1 - i) / k;
        if ((h - 1 - i) % k != 0) down++;
        cnt = min({ri, le, up, down});
        ans = min(ans, cnt + 1);
      }
    }
  }
  cout << (ans) << endl;
  return 0;
}
