#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long int to, cost;
};
std::vector<std::vector<edge> > G(1000000);
priority_queue<pair<long long int, long long int>,
               std::vector<pair<long long int, long long int> >,
               greater<pair<long long int, long long int> > >
    que;
std::vector<long long int> d(1000000 + 1, 1000000009);
void dijkstra(int start) {
  d[start] = 0;
  que.push(pair<long long int, long long int>(0, start));
  while (!que.empty()) {
    pair<long long int, long long int> p_now = que.top();
    que.pop();
    long long int v = p_now.second;
    if (d[v] < p_now.first) continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pair<long long int, long long int>(d[e.to], e.to));
      }
    }
  }
}
int main(int argc, char const *argv[]) {
  int H, W, K, ans = 0, dist = 1000000007;
  int N = 810;
  pair<int, int> Start;
  std::cin >> H >> W >> K;
  std::vector<string> room(H);
  for (int i = 0; i < H; i++) {
    std::cin >> room[i];
    for (int j = 0; j < W; j++)
      if (room[i][j] == 'S') Start = make_pair(i, j);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (room[i][j] != '#') {
        if ((i == 0 ? false : room[i - 1][j] != '#'))
          G[i * H + j].push_back(edge{(i - 1) * N + j, 1});
        if ((j == 0 ? false : room[i][j - 1] != '#'))
          G[i * H + j].push_back(edge{i * N + (j - 1), 1});
        if ((i == H - 1 ? false : room[i + 1][j] != '#'))
          G[i * H + j].push_back(edge{(i + 1) * N + j, 1});
        if ((j == W - 1 ? false : room[i][j + 1] != '#'))
          G[i * H + j].push_back(edge{i * N + (j + 1), 1});
      }
    }
  }
  dijkstra(Start.first * N + Start.second);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (d[i * N + j] <= K) {
        dist = min({dist, i, j, W - j - 1, H - 1 - i});
      }
    }
  }
  ans = 1 + dist / K + (dist % K != 0 ? 1 : 0);
  std::cout << ans << '\n';
  return 0;
}
