#include <bits/stdc++.h>
using namespace std;
unsigned long long inf = (1LL << 62);
long long MOD = (long long)pow(10.0, 9.0) + 7;
pair<long long, long long> dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
long long min(long long a, long long b) { return (a < b) ? a : b; }
long long max(long long a, long long b) { return (a > b) ? a : b; }
template <typename T>
struct edge {
  int src, to;
  T cost;
  edge(int to, T cost) : src(-1), to(to), cost(cost) {}
  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
  bool operator<(const edge<T> e1) { return cost < e1.cost; }
  edge &operator=(const int &x) {
    to = x;
    return *this;
  }
  operator int() const { return to; }
};
template <typename T>
using Edge = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edge<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T>
using Matrix = vector<vector<T>>;
template <typename T>
vector<T> dijkstra(WeightedGraph<T> &g, int s) {
  const auto INF = numeric_limits<T>::max();
  vector<T> dist(g.size(), INF);
  using Pi = pair<T, int>;
  priority_queue<Pi, vector<Pi>, greater<Pi>> q;
  dist[s] = 0;
  q.emplace(dist[s], s);
  while (!q.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = q.top();
    q.pop();
    if (dist[idx] < cost) continue;
    for (auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if (dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      q.emplace(dist[e.to], e.to);
    }
  }
  return dist;
}
long long H, W, K;
char A[805][805];
int main() {
  cin >> H >> W >> K;
  WeightedGraph<long long> e(H * W + 1), e2(H * W + 1);
  long long sx, sy;
  auto in = [&](int x, int y) { return x >= 0 && y >= 0 && x < H && y < W; };
  auto idx = [&](int x, int y) { return W * x + y; };
  for (int i = 0; i < (int)(H); i++)
    for (int j = 0; j < (int)(W); j++) cin >> A[i][j];
  for (int i = 0; i < (int)(H); i++) {
    for (int j = 0; j < (int)(W); j++) {
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      for (int k = 0; k < (int)(4); k++) {
        int nx = i + dx[k].first, ny = j + dx[k].second;
        if (in(nx, ny))
          e[idx(i, j)].emplace_back(idx(nx, ny), ((A[nx][ny] == '#') ? 2 : 1));
      }
    }
  }
  vector<long long> dist1 = dijkstra(e, idx(sx, sy));
  for (int i = 0; i < (int)(H); i++) {
    for (int j = 0; j < (int)(W); j++) {
      if (A[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      for (int k = 0; k < (int)(4); k++) {
        int nx = i + dx[k].first, ny = j + dx[k].second;
        if (in(nx, ny) && A[nx][ny] != '#' && A[i][j] != '#')
          e2[idx(i, j)].emplace_back(idx(nx, ny), 1);
      }
    }
  }
  vector<long long> dist2 = dijkstra(e2, idx(sx, sy));
  long long m1 = inf, m2 = inf;
  for (int i = 0; i < (int)(W); i++) {
    m1 = min(m1, dist1[idx(0, i)]);
    m1 = min(m1, dist1[idx(H - 1, i)]);
    m2 = min(m2, dist2[idx(0, i)]);
    m2 = min(m2, dist2[idx(H - 1, i)]);
  }
  for (int i = 0; i < (int)(H); i++) {
    m1 = min(m1, dist1[idx(i, 0)]);
    m1 = min(m1, dist1[idx(i, W - 1)]);
    m2 = min(m2, dist2[idx(i, 0)]);
    m2 = min(m2, dist2[idx(i, W - 1)]);
  }
  cout << min(max(2, m1 / K), m2 / K + 1) << endl;
  return 0;
}
