#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    if (1) {
      ios::sync_with_stdio(false);
      cin.tie(0);
    }
  }
} star;
template <typename T>
inline bool chmin(T &l, T r) {
  bool a = l > r;
  if (a) l = r;
  return a;
}
template <typename T>
inline bool chmax(T &l, T r) {
  bool a = l < r;
  if (a) l = r;
  return a;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (auto &it : v) is >> it;
  return is;
}
vector<string> wrap(vector<string> s, char c) {
  int R = s.size();
  int C = s[0].size();
  vector<string> res(R + 2, string(C + 2, c));
  for (int i = (0); i < (R); i++)
    for (int j = (0); j < (C); j++) res[i + 1][j + 1] = s[i][j];
  return res;
}
int ceil(int a, int b) { return (a + b - 1) / b; }
int dr[] = {0, 1, 0, -1};
int dc[] = {1, 0, -1, 0};
int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<string> S(R);
  cin >> S;
  S = wrap(S, '#');
  R += 2;
  C += 2;
  int sr, sc;
  for (int i = (0); i < (R); i++)
    for (int j = (0); j < (C); j++)
      if (S[i][j] == 'S') sr = i, sc = j;
  vector<vector<int> > cost(R, vector<int>(C, 114514));
  int res = 114514;
  queue<pair<int, int> > que;
  que.push(pair<int, int>(sr, sc));
  cost[sr][sc] = 0;
  while (que.size()) {
    sr = que.front().first;
    sc = que.front().second;
    que.pop();
    for (int i = (0); i < (4); i++) {
      int nr = sr + dr[i];
      int nc = sc + dc[i];
      int ncost = cost[sr][sc] + 1;
      if (S[nr][nc] != '#' && chmin(cost[nr][nc], ncost)) {
        que.push(pair<int, int>(nr, nc));
      }
    }
  }
  R--;
  C--;
  for (int i = (1); i < (R); i++)
    for (int j = (1); j < (C); j++)
      if (cost[i][j] <= K)
        chmin(res, 1 + ceil(min({i - 1, j - 1, R - i, C - j}), K));
  cout << res << endl;
  return 0;
}
