#include <bits/stdc++.h>
using i64 = long long;
int main() {
  int h, w, k;
  std::cin >> h >> w >> k;
  std::vector<std::string> board(h);
  std::vector<std::vector<int>> g(h * w);
  constexpr int inf = 1000000000;
  std::vector<int> dist(h * w, inf);
  for (int i = 0; i < h; i++) std::cin >> board[i];
  std::deque<int> q;
  int si;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      int index = i * w + j;
      if (board[i][j] != '#') {
        dist[index] = 0;
        q.push_back(index);
        if (board[i][j] == 'S') si = index;
      }
      if (i > 0) g[index].push_back((i - 1) * w + j);
      if (i + 1 < h) g[index].push_back((i + 1) * w + j);
      if (j > 0) g[index].push_back(i * w + j - 1);
      if (j + 1 < w) g[index].push_back(i * w + j + 1);
    }
  while (!q.empty()) {
    int v = q.front();
    q.pop_front();
    for (int to : g[v])
      if (dist[to] > dist[v] + 1) {
        dist[to] = dist[v] + 1;
        q.push_back(to);
      }
  }
  int l = 0, r = 888;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    std::vector<std::vector<int>> g2(g.size());
    std::vector<int> d(dist.size(), inf);
    d[si] = 0;
    for (int i = 0; i < g2.size(); i++) {
      for (int to : g[i]) {
        if (std::max(dist[i], dist[to]) <= k * (mid - 1)) {
          g2[i].push_back(to);
          g2[to].push_back(i);
        }
      }
    }
    std::deque<int> q{si};
    while (!q.empty()) {
      int v = q.front();
      q.pop_front();
      for (int to : g2[v]) {
        if (d[to] > d[v] + 1) {
          d[to] = d[v] + 1;
          q.push_back(to);
        }
      }
    }
    bool flg = false;
    for (int i = 0; i < w; i++)
      if (std::min(d[i], d[(h - 1) * w + i]) <= mid * k) flg = true;
    for (int i = 0; i < h; i++)
      if (std::min(d[i * w], d[i * w + w - 1]) <= mid * k) flg = true;
    if (flg)
      r = mid;
    else
      l = mid;
  }
  std::cout << r << std::endl;
  return 0;
}
