#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
long long mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
long long llmax = 9223372036854775807;
long long llmin = -9223372036854775807;
int iinf = intmax / 8;
long long inf = llmax / 8;
double eps = 1e-11;
long long H, W, K;
bool bo[1000][1000];
long long boone[1000][1000];
long long num[1000][1000];
void BFS01(long long sth, long long stw) {
  deque<pair<long long, long long> > que;
  num[sth][stw] = 0;
  que.push_front(pair<long long, long long>(sth, stw));
  while (que.size() > 0) {
    pair<long long, long long> base = que.front();
    long long basex = base.first;
    long long basey = base.second;
    que.pop_front();
    for (long long i = 0; i < 4; i++) {
      long long x = basex + dx[i];
      long long y = basey + dy[i];
      if (x >= 0 && x <= H && y >= 0 && y <= H) {
        if (num[x][y] == -1) {
          if (boone[basex][basey] >= 0 && boone[x][y] >= 0) {
            num[x][y] = num[basex][basey];
            que.push_front(pair<long long, long long>(x, y));
          } else {
            num[x][y] = num[basex][basey] + 1;
            que.push_back(pair<long long, long long>(x, y));
          }
        }
      }
    }
  }
}
long long calc(long long x) { return (x + K - 1) / K + 1; }
int main() {
  cin >> H >> W >> K;
  long long sth, stw;
  for (long long i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (long long j = 0; j < W; j++) {
      if (s[j] == 'S') {
        sth = i;
        stw = j;
        bo[i][j] = true;
      } else if (s[j] == '.') {
        bo[i][j] = true;
      } else {
        bo[i][j] = false;
      }
    }
  }
  for (long long i = 0; i < 1000; i++) {
    fill(boone[i], boone[i] + 1000, -1);
    fill(num[i], num[i] + 1000, -1);
  }
  queue<pair<long long, long long> > que;
  que.push(pair<long long, long long>(sth, stw));
  boone[sth][stw] = 0;
  while (!que.empty()) {
    pair<long long, long long> p = que.front();
    que.pop();
    long long tmp = boone[p.first][p.second];
    if (tmp < K) {
      for (long long i = 0; i < 4; i++) {
        long long x = p.first + dx[i];
        long long y = p.second + dy[i];
        if (x >= 0 && x <= H && y >= 0 && y <= W) {
          if (bo[x][y] && boone[x][y] == -1) {
            boone[x][y] = tmp + 1;
            que.push(pair<long long, long long>(x, y));
          }
        }
      }
    }
  }
  BFS01(sth, stw);
  long long ans = 1e7;
  for (long long i = 0; i < H; i++) {
    ans = min(ans, calc(num[i][0]));
    ans = min(ans, calc(num[i][W - 1]));
  }
  for (long long i = 0; i < W; i++) {
    ans = min(ans, calc(num[0][i]));
    ans = min(ans, calc(num[H - 1][i]));
  }
  cout << ans << endl;
}
