#include <bits/stdc++.h>
#pragma warning(disable : 4996)
const long long MOD = 1000000007;
using namespace std;
char str[800][805];
int main(int argc, char* argv[]) {
  int h, w, K;
  scanf("%d%d%d", &h, &w, &K);
  vector<int> vis(h * w);
  int si = -1, sj = -1;
  int i, j;
  for (i = 0; i < h; i++) {
    scanf("%s", str[i]);
    for (j = 0; j < w; j++) {
      if (str[i][j] == 'S') {
        si = i;
        sj = j;
      }
    }
  }
  assert(si >= 0 && sj >= 0);
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  queue<pair<int, int> > que0;
  que0.push(make_pair(((si)*w + (sj)), 0));
  int min = 2140000000;
  while (!que0.empty()) {
    auto tmp = que0.front();
    int curr = tmp.first;
    int dist = tmp.second;
    que0.pop();
    int i = curr / w;
    int j = curr % w;
    vis[((i)*w + (j))] = 1;
    if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
      printf("1\n");
      return 0;
    }
    min = ((min) > (i) ? (i) : (min));
    min = ((min) > (h - 1 - i) ? (h - 1 - i) : (min));
    min = ((min) > (j) ? (j) : (min));
    min = ((min) > (w - 1 - j) ? (w - 1 - j) : (min));
    int p;
    for (p = 0; p < 4; p++) {
      int i2 = i + dx[p];
      int j2 = j + dy[p];
      int dist2 = dist + 1;
      if (i2 < 0 || i2 >= h || j2 < 0 || j2 >= w) continue;
      if (dist < K && str[i2][j2] == '.' && !vis[((i2)*w + (j2))]) {
        int dist2 = dist + 1;
        que0.push(make_pair(((i2)*w + (j2)), dist2));
      }
    }
  }
  printf("%d\n", (min - 1 + K) / K + 1);
  return 0;
}
