#include <bits/stdc++.h>
using namespace std;
template <class T>
using vv = vector<vector<T> >;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
vector<vector<int> > board;
int start_x, start_y;
int h, w, k;
int min_distance(int y, int x) {
  int ret = 1e9;
  ret = min(ret, x);
  ret = min(ret, w - 1 - x);
  ret = min(ret, y);
  ret = min(ret, h - 1 - y);
  return ret;
}
int main() {
  cin >> h >> w >> k;
  board.assign(h, vector<int>(w, 0));
  for (int(i) = (0); (i) < (h); ++(i)) {
    char tmp;
    for (int(j) = (0); (j) < (w); ++(j)) {
      scanf(" %c", &tmp);
      if (tmp == '#') {
        board[i][j] = -1;
      }
      if (tmp == 'S') {
        start_y = i;
        start_x = j;
      }
    }
  }
  vector<vector<int> > cost(h, vector<int>(w, 1e9));
  vv<bool> reached(h, vector<bool>(w, false));
  deque<pair<int, int> > stk;
  stk.push_back((pair<int, int>){start_y, start_x});
  cost[start_y][start_x] = 0;
  reached[start_y][start_x] = true;
  while (!stk.empty()) {
    auto u = stk.front();
    stk.pop_front();
    for (int(i) = (0); (i) < (4); ++(i)) {
      int y = u.first + dy[i];
      int x = u.second + dx[i];
      if (x < 0 || w <= x || y < 0 || h <= y) {
        continue;
      }
      if (board[y][x] == -1) {
        continue;
      }
      if (reached[y][x]) {
        continue;
      }
      cost[y][x] = min(cost[y][x], cost[u.first][u.second] + 1);
      reached[y][x] = true;
    }
  }
  int min_cost = 1e9;
  for (int(i) = (0); (i) < (h); ++(i)) {
    for (int(j) = (0); (j) < (w); ++(j)) {
      if (cost[i][j] <= k) {
        min_cost = min(min_cost, min_distance(i, j));
      }
    }
  }
  int ans = 1 + (min_cost + k - 1) / k;
  printf("%d\n", ans);
  return 0;
}
