#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
  static int h, w, k;
  static char a[802][802];
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; i++) {
    scanf("\n");
    for (int j = 1; j <= w; j++) scanf("%c", &a[i][j]);
  }
  static int d[802][802];
  static bool used[802][802];
  static queue<pair<int, int> > que;
  for (int i = 0; i < 802; i++)
    for (int j = 0; j < 802; j++) {
      d[i][j] = INF;
      used[i][j] = false;
    }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (a[i][j] == 'S') {
        d[i][j] = 0;
        que.push(pair<int, int>(i, j));
      }
    }
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    if (used[p.first][p.second]) continue;
    used[p.first][p.second] = true;
    for (int i = 0; i < 4; i++) {
      if (a[p.first + dx[i]][p.second + dy[i]] == '#') continue;
      d[p.first + dx[i]][p.second + dy[i]] = d[p.first][p.second] + 1;
      if (d[p.first + dx[i]][p.second + dy[i]] < k)
        que.push(pair<int, int>(p.first + dx[i], p.second + dy[i]));
    }
  }
  int ret = INF;
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      if (d[i][j] == INF) continue;
      int D = min(i - 1, h - i);
      D = min(D, min(j - 1, w - j));
      ret = min(ret, (D + k - 1) / k + 1);
    }
  cout << ret << endl;
}
