#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool maxup(T& a, const T&& b) {
  if (a < b) {
    a = b;
    return true;
  };
}
template <typename T>
bool maxup(T& a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  };
}
template <typename T>
bool minup(T& a, const T&& b) {
  if (a > b) {
    a = b;
    return true;
  };
}
template <typename T>
bool minup(T& a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  };
}
using VV = vector<vector<long long>>;
using V = vector<long long>;
using P = pair<long long, long long>;
using IP = pair<long long, P>;
template <typename T>
inline void input(vector<T>& v) {
  for (auto& x : v) cin >> x;
}
bool stage[800][800];
vector<P> starts;
long long dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
bool visited[800][800];
long long H, W, K;
long long bfs() {
  memset(visited, 0, sizeof(visited));
  using T = tuple<long long, long long, long long>;
  queue<T> que;
  for (const P p : starts) {
    long long r, c;
    tie(r, c) = p;
    que.push(T(r, c, 0));
    visited[r][c] = true;
  }
  while (!que.empty()) {
    long long r, c, cnt;
    tie(r, c, cnt) = que.front();
    que.pop();
    for (signed i = 0; i < signed(4); i++) {
      long long nr = dr[i] + r, nc = dc[i] + c;
      if (nr < 1 || nc < 1 || nr >= H - 1 || nc >= W - 1) {
        return cnt + 1;
      }
      if (visited[nr][nc]) continue;
      visited[nr][nc] = true;
      que.push(T(nr, nc, cnt + 1));
    }
  }
}
void calc() {
  cin >> H >> W >> K;
  long long sr, sc;
  for (signed i = 0; i < signed(H); i++)
    for (signed j = 0; j < signed(W); j++) {
      char c;
      cin >> c;
      if (c == 'S') {
        sr = i;
        sc = j;
      }
      stage[i][j] = c != '#';
    }
  vector<vector<bool>> memo(H, vector<bool>(W));
  starts.push_back(P(sr, sc));
  memo[sr][sc] = true;
  queue<P> que;
  que.push(P(sr, sc));
  while (!que.empty()) {
    long long r, c;
    tie(r, c) = que.front();
    que.pop();
    for (signed i = 0; i < signed(4); i++) {
      long long nr = dr[i] + r, nc = dc[i] + c;
      if (!stage[nr][nc]) continue;
      if (nr < 1 || nc < 1 || nr >= H - 1 || nc >= W - 1) {
        cout << 1 << endl;
        return;
      }
      if (memo[nr][nc]) continue;
      memo[nr][nc] = true;
      starts.push_back(P(nr, nc));
      que.push(P(nr, nc));
    }
  }
  long long tmp = bfs();
  cout << 1 + (bfs() - 1) / K + 1 << endl;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  calc();
}
