#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
constexpr int vx[] = {1, 0, -1, 0}, vy[] = {0, 1, 0, -1};
std::vector<std::string> s;
int h, w, k, sx, sy;
int dist(int x, int y) {
  std::queue<std::pair<int, int>> qu;
  qu.push({x, y});
  std::vector<std::vector<int>> min_cost(h, std::vector<int>(w, 1e9));
  min_cost[x][y] = 0;
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + vy[i], ny = p.second + vx[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
      if (min_cost[p.first][p.second] + 1 >= min_cost[nx][ny]) continue;
      min_cost[nx][ny] = min_cost[p.first][p.second] + 1;
      qu.push({nx, ny});
    }
  }
  int ret = 1e9;
  for (int i = 0; i < w; i++) {
    ret = std::min(ret, min_cost[0][i]);
    ret = std::min(ret, min_cost[h - 1][i]);
  }
  for (int i = 0; i < h; i++) {
    ret = std::min(ret, min_cost[i][0]);
    ret = std::min(ret, min_cost[i][w - 1]);
  }
  return (ret + k - 1) / k;
}
int main() {
  cin >> h >> w >> k;
  s.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
    for (int j = 0; j < w; j++) {
      if (s[i][j] != 'S') continue;
      sx = i;
      sy = j;
      break;
    }
  }
  int mi = dist(sx, sy) + 1;
  std::queue<std::pair<int, std::pair<int, int>>> qu;
  qu.push({k, {sx, sy}});
  std::vector<std::vector<bool>> used(h, std::vector<bool>(w, false));
  used[sx][sy] = true;
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.second.first + vy[i], ny = p.second.second + vx[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w || used[nx][ny] ||
          s[nx][ny] == '#')
        continue;
      mi = std::min(mi, 1 + dist(nx, ny));
      used[nx][ny] = true;
      if (p.first) qu.push({p.first - 1, {nx, ny}});
    }
  }
  cout << mi << endl;
  return 0;
}
