#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _R(T &x) {
  cin >> x;
}
void _R(int &x) { scanf("%d", &x); }
void _R(long long &x) { cin >> x; }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template <typename T, typename... U>
void R(T &head, U &...tail) {
  _R(head);
  R(tail...);
}
template <typename T>
void _W(const T &x) {
  cout << x;
}
void _W(const int &x) { printf("%d", x); }
template <typename T>
void _W(const vector<T> &x) {
  for (auto i = x.cbegin(); i != x.cend(); i++) {
    if (i != x.cbegin()) putchar(' ');
    _W(*i);
  }
}
void W() {}
template <typename T, typename... U>
void W(const T &head, const U &...tail) {
  _W(head);
  putchar(sizeof...(tail) ? ' ' : '\n');
  W(tail...);
}
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846264338327950288L;
const double eps = 1e-6;
const int mo = 1e9 + 7;
char grid[805][805];
bool vis[805][805];
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int h, w, si, sj;
double k;
int reach(int i, int j) {
  return min(ceil(min(i, h - j - 1) / k), ceil(min(j, w - j - 1) / k));
}
int toSrc(int i, int j) { return abs(si - i) + abs(sj - j); }
bool hasExit(int i, int j) {
  return i == 0 || i == (h - 1) || j == 0 || j == (w - 1);
}
int main() {
  R(h, w, k);
  for (int i = (0); i <= int(h - 1); i++)
    for (int j = (0); j <= int(w - 1); j++) {
      R(grid[i][j]);
      if (grid[i][j] == 'S') si = i, sj = j;
    }
  int ans = inf;
  queue<pair<int, int> > q;
  q.push(pair<int, int>(si, sj));
  vector<pair<int, int> > v;
  v.push_back(pair<int, int>(si, sj));
  memset(vis, false, sizeof vis);
  vis[si][sj] = true;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    for (int it = (0); it <= int(3); it++) {
      int ni = p.first + dr[it], nj = p.second + dc[it];
      if (!vis[ni][nj] && grid[ni][nj] == '.' && toSrc(ni, nj) <= k) {
        q.push(pair<int, int>(ni, nj));
        v.push_back(pair<int, int>(ni, nj));
        vis[ni][nj] = true;
      }
    }
  }
  for (int i = (0); i <= int(((int)(v).size()) - 1); i++) {
    pair<int, int> p = v[i];
    if (hasExit(p.first, p.second))
      ans = 1;
    else
      ans = min(ans, 1 + reach(p.first, p.second));
  }
  W(ans);
  return 0;
}
