#include <bits/stdc++.h>
using namespace std;
template <class T>
void read(T &res) {
  res = 0;
  T f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T>
void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
int H, W, K;
char s[805][805];
bool vis[805][805];
int dis[805][805];
queue<pair<int, int> > Q;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void Solve() {
  read(H);
  read(W);
  read(K);
  for (int i = 1; i <= H; ++i) {
    scanf("%s", s[i] + 1);
  }
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (s[i][j] == 'S') {
        Q.push(make_pair(i, j));
        vis[i][j] = 1;
      }
    }
  }
  while (!Q.empty()) {
    pair<int, int> u = Q.front();
    Q.pop();
    if (dis[u.first][u.second] == K) continue;
    for (int k = 0; k < 4; ++k) {
      int tx = u.first + dx[k], ty = u.second + dy[k];
      if (s[tx][ty] == '.' && !vis[tx][ty]) {
        Q.push(make_pair(tx, ty));
        vis[tx][ty] = 1;
        dis[tx][ty] = dis[u.first][u.second] + 1;
      }
    }
  }
  int ans = 1000000000;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      if (vis[i][j]) {
        if (i == 1 || j == 1)
          ans = min(ans, 1);
        else {
          int t = min(i, j) - 1;
          ans = min(ans, 1 + (t - 1) / K + 1);
        }
      }
    }
  }
  out(ans);
  putchar('\n');
}
int main() { Solve(); }
