#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
template <class T>
bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v) is >> a;
  return is;
}
const int INF = 1 << 30;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
class Solver {
 public:
  bool solve() {
    int H, W, K;
    cin >> H >> W >> K;
    vector<string> G(H);
    cin >> G;
    int sx = -1, sy = -1;
    for (int i = 0; i < (int)(H); i++)
      for (int j = 0; j < (int)(W); j++)
        if (G[i][j] == 'S') {
          sy = i;
          sx = j;
          goto OK;
        }
  OK:
    assert(sx >= 0 and sy >= 0);
    auto eval = [&](pii p) {
      return min({p.first, H - p.first - 1, p.second, W - p.second - 1});
    };
    auto div = [&](int a) { return a / K + (a % K != 0); };
    auto calc_cost = [&](pii pos, int c) -> int {
      return div(eval(pos)) + div(c) + (pos.first == sy and pos.second == sx);
    };
    queue<pair<pii, int>> que;
    que.push(make_pair(make_pair(sy, sx), 0));
    vector<vector<char>> visited(H, vector<char>(W, false));
    int ans = INF;
    while (que.size() > 0) {
      pii now = que.front().first;
      int cost = que.front().second;
      que.pop();
      visited[now.first][now.second] = true;
      set_min(ans, calc_cost(now, cost));
      for (int i = 0; i < (int)(4); i++) {
        int nx = now.second + dx[i], ny = now.first + dy[i];
        if (0 <= ny and ny < H and 0 <= nx and nx < W) {
          if (not visited[ny][nx] and G[ny][nx] == '.') {
            que.push(make_pair(make_pair(ny, nx), cost + 1));
          }
        }
      }
    }
    cout << ans << endl;
    return 0;
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Solver s;
  s.solve();
  return 0;
}
