#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec) is >> x;
  return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "{";
  for (int i = 0; i < (int)vec.size(); i++)
    os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
  os << "}";
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
  os << "{";
  for (auto &&itr : map_var) {
    os << *itr;
    itr++;
    if (itr != map_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
  os << "{";
  for (auto &&itr : set_var) {
    os << *itr;
    itr++;
    if (itr != set_var.end()) os << ", ";
    itr--;
  }
  os << "}";
  return os;
}
void dump_func() { cerr << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
  cerr << head;
  if (sizeof...(Tail) > 0) cerr << ", ";
  dump_func(std::move(tail)...);
}
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
using vec = std::vector<T>;
template <class T>
void print(const T &x) {
  cout << x << "\n";
}
const long long MOD = 1000000007, INF0 = 1061109567, INF = INF0 * INF0;
const double EPS = 1e-10, PI = acos(-1.0);
const long long dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct edge {
  long long from, to;
};
char A[800][800];
long long H, W, K, S;
long long d[640000];
long long d2[640000];
vector<long long> G[640000];
bool visited[640000];
struct Idcost {
  long long id, cost;
};
queue<Idcost> q;
void bfs() {
  fill(d, d + H * W, INF);
  Idcost start = {S, 0};
  q.push(start);
  while (!q.empty()) {
    Idcost now = q.front();
    d[now.id] = now.cost;
    visited[now.id] = true;
    q.pop();
    for (auto x : G[now.id]) {
      Idcost next = {x, now.cost + 1};
      if (!visited[x]) {
        q.push(next);
      }
    }
  }
  return;
}
signed main() {
  cin.tie(0), ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  cin >> H >> W >> K;
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++) {
      cin >> A[i][j];
      if (A[i][j] == 'S') S = i * W + j;
    }
  for (long long i = 0; i < H; i++) {
    for (long long j = 0; j < W - 1; j++) {
      if (A[i][j] != '#' && A[i][j + 1] != '#') {
        long long addr = i * W + j;
        G[addr].push_back(addr + 1);
        G[addr + 1].push_back(addr);
      }
    }
  }
  for (long long i = 0; i < W; i++) {
    for (long long j = 0; j < H - 1; j++) {
      if (A[j][i] != '#' && A[j + 1][i] != '#') {
        long long addr = j * W + i;
        G[addr].push_back(addr + W);
        G[addr + W].push_back(addr);
      }
    }
  }
  bfs();
  fill(d2, d2 + H * W, INF);
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++) {
      long long val = min({i, H - 1 - i, j, W - 1 - j});
      d2[i * W + j] = (val + K - 1) / K;
    }
  long long ans = INF;
  for (long long i = 0; i < H; i++)
    for (long long j = 0; j < W; j++) {
      if (d[i * W + j] <= K) chmin(ans, 1 + d2[i * W + j]);
    }
  print(ans);
}
