#include <bits/stdc++.h>
using namespace std;
int h, w, k;
vector<string> brd;
int dist[890][890];
int dxy[] = {1, 0, -1, 0, 1};
auto ceil_m = [](int a, int b) { return (a - 1) / b + 1; };
int main(void) {
  cin >> h >> w >> k;
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    brd.push_back(s);
  }
  int sx, sy;
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j) {
      if (brd[i][j] == 'S') {
        brd[i][j] = '.';
        sx = i;
        sy = j;
      }
    }
  memset(dist, 0xff, sizeof(dist));
  int ans = INT_MAX;
  queue<pair<pair<int, int>, int>> que;
  dist[sx][sy] = 0;
  que.push({{sx, sy}, 0});
  while (!que.empty()) {
    auto par = que.front();
    que.pop();
    int i = par.first.first;
    int j = par.first.second;
    int dis = par.second;
    if (i < 0 || j < 0 || i >= h || j >= w) {
      int can = max(1, ceil_m(dis, k));
      ans = min(ans, can);
      continue;
    }
    if (brd[i][j] == '#') {
      int P = min({h - i, i + 1, w - j, j + 1});
      int can = max(ceil_m(P, k) + 1, ceil_m(dis + P - 1, k));
      ans = min(ans, can);
      continue;
    }
    for (int dir = 0; dir < 4; ++dir) {
      int ii = i + dxy[dir];
      int jj = j + dxy[dir + 1];
      if (dist[ii][jj] < 0) {
        dist[ii][jj] = dis + 1;
        que.push({{ii, jj}, dis + 1});
      }
    }
  }
  cout << ans << endl;
  return 0;
}
