#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long int linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
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 <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i];
    if (i + 1 != vec.size()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &st) {
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    os << *itr;
    auto titr = itr;
    if (++titr != st.end()) os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << itr->first << ":" << itr->second;
    auto titr = itr;
    if (++titr != mp.end()) os << " ";
  }
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) {
  for (auto itr = mp.begin(); itr != mp.end(); ++itr) {
    os << itr->first << ":" << itr->second;
    auto titr = itr;
    if (++titr != mp.end()) os << " ";
  }
  return os;
}
int h, w, k;
string s[801];
struct state {
  int i, j, k, m;
  bool b;
  state() {}
  state(int i, int j, int k, int m, bool b = false)
      : i(i), j(j), k(k), m(m), b(b) {}
  bool operator<(const state &r) const {
    if (m != r.m) return m > r.m;
    if (k != r.k) return k > r.k;
    return !b;
  }
};
int dis[801][801];
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  cin >> h >> w >> k;
  for (int i = (0); i < (h); ++i) cin >> s[i];
  int sx = 0, sy = 0;
  for (int i = (0); i < (h); ++i)
    for (int j = (0); j < (w); ++j)
      if (s[i][j] == 'S') sx = i, sy = j;
  priority_queue<state> priority_queue;
  for (int i = (0); i < (h); ++i)
    for (int j = (0); j < (w); ++j) dis[i][j] = inf;
  dis[sx][sy] = 1;
  priority_queue.push(state(sx, sy, 1, 1));
  while (!priority_queue.empty()) {
    auto p = priority_queue.top();
    priority_queue.pop();
    if (dis[p.i][p.j] < p.m) continue;
    for (int l = (0); l < (4); ++l) {
      int nx = p.i + dx[l];
      int ny = p.j + dy[l];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
        if (s[nx][ny] != '#') {
          state s = state(nx, ny, p.k + 1, p.m, p.b);
          if (s.k > k) {
            s.m++;
            s.k = 1;
            s.b = false;
          }
          if (dis[nx][ny] > s.m) {
            priority_queue.push(s);
            dis[nx][ny] = s.m;
          }
        } else {
          state s = state(nx, ny, p.k + 1, p.m, p.b);
          if (!p.b) {
            s.k = 1;
            s.m++;
            s.b = true;
          }
          if (s.k > k) {
            s.k = 1;
            s.m++;
            s.b = false;
          }
          if (dis[nx][ny] > s.m) {
            priority_queue.push(s);
            dis[nx][ny] = s.m;
          }
        }
      }
    }
  }
  int res = inf;
  for (int i = (0); i < (h); ++i) {
    chmin(res, dis[i][0]);
    chmin(res, dis[i][w - 1]);
  }
  for (int j = (0); j < (w); ++j) {
    chmin(res, dis[0][j]);
    chmin(res, dis[h - 1][j]);
  }
  cout << res << endl;
  return 0;
}
