#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)1e9 + 7;
const int MAXN = 1e5 + 5;
const long long INF = 1LL << 30;
const long double eps = 1e-10;
const double PI = 3.1415926535897932;
char s[800][800];
int dist1[801][801];
int dist2[801][801];
queue<pair<int, int> > q1;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};
int main() {
  int h, w, k;
  scanf("%d%d%d", &h, &w, &k);
  memset((dist1), -1, sizeof((dist1)));
  for (int i = 0; i < (h); ++i) {
    scanf("%s", (s[i]));
    for (int j = 0; j < (w); ++j) {
      if (s[i][j] == 'S') {
        dist1[i][j] = 0;
        q1.push(make_pair(i, j));
      }
    }
  }
  int minz = 100000;
  while (((int)(q1).size())) {
    pair<int, int> cur = q1.front();
    q1.pop();
    int x = cur.first, y = cur.second;
    if (x == 0 || y == 0 || x == h - 1 || y == w - 1) {
      minz = min(minz, dist1[x][y]);
    }
    for (int i = 0; i < (4); ++i) {
      int nx = cur.first + dx[i], ny = cur.second + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
      if (s[nx][ny] == '#') continue;
      if (dist1[nx][ny] != -1) continue;
      dist1[nx][ny] = dist1[x][y] + 1;
      q1.push(make_pair(nx, ny));
    }
  }
  if (minz <= k) {
    printf("1");
    return 0;
  }
  memset((dist2), -1, sizeof((dist2)));
  for (int i = 0; i < (h); ++i) {
    for (int j = 0; j < (w); ++j) {
      if (dist1[i][j] != -1 && dist1[i][j] <= k) {
        dist2[i][j] = 0;
        q1.push(make_pair(i, j));
      }
    }
  }
  minz = 100000;
  while (((int)(q1).size())) {
    pair<int, int> cur = q1.front();
    q1.pop();
    int x = cur.first, y = cur.second;
    if (x == 0 || y == 0 || x == h - 1 || y == w - 1) {
      minz = min(minz, dist2[x][y]);
      break;
    }
    for (int i = 0; i < (4); ++i) {
      int nx = cur.first + dx[i], ny = cur.second + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
      if (dist2[nx][ny] != -1) continue;
      dist2[nx][ny] = dist2[x][y] + 1;
      q1.push(make_pair(nx, ny));
    }
  }
  printf("%d", (minz + k - 1) / k + 1);
}
