#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = pair<ll, pll>;
const ll LINF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
const ll MODD = 0x3b800001;
const int INF = 0x3fffffff;
const double DINF = numeric_limits<double>::infinity();
const vector<pii> four = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
__attribute__((constructor)) void SETTINGS() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
};
template <class T>
inline constexpr T gcd(T a, T b) {
  if (!a || !b) return 0;
  return a % b ? gcd(b, a % b) : b;
}
template <class T>
inline constexpr T lcm(T a, T b) {
  return a * b / gcd(a, b);
}
template <class T>
inline constexpr T min(vector<T>& v) {
  return *min_element((v).begin(), (v).end());
}
inline char min(string& v) { return *min_element((v).begin(), (v).end()); }
template <class T>
inline constexpr T max(vector<T>& v) {
  return *max_element((v).begin(), (v).end());
}
inline char max(string& v) { return *max_element((v).begin(), (v).end()); }
inline ll pow(ll a, ll b) {
  if (b == 0) return 1;
  ll ans = pow(a, b / 2);
  return ans * ans * (b & 1 ? a : 1);
}
inline ll modpow(ll a, ll b, ll mod) {
  if (b == 0) return 1;
  ll ans = pow(a, b / 2);
  return ans * ans * (b & 1 ? a : 1) % mod;
}
template <typename T>
inline bool update_min(T& mn, const T& cnt) {
  if (mn > cnt) {
    mn = cnt;
    return 1;
  } else
    return 0;
}
template <typename T>
inline bool update_max(T& mx, const T& cnt) {
  if (mx < cnt) {
    mx = cnt;
    return 1;
  } else
    return 0;
}
inline void in() {}
template <class T>
istream& operator>>(istream& is, vector<T>& vec);
template <class T, size_t size>
istream& operator>>(istream& is, array<T, size>& vec);
template <class T, class L>
istream& operator>>(istream& is, pair<T, L>& p);
template <class T>
ostream& operator<<(ostream& os, vector<T>& vec);
template <class T, class L>
ostream& operator<<(ostream& os, pair<T, L>& p);
template <class T>
istream& operator>>(istream& is, vector<T>& vec) {
  for (T& x : vec) is >> x;
  return is;
}
template <class T, class L>
istream& operator>>(istream& is, pair<T, L>& p) {
  is >> p.first;
  is >> p.second;
  return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& vec) {
  os << vec[0];
  for (ll i = 1; i < vec.size(); ++i) {
    os << ' ' << vec[i];
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, deque<T>& deq) {
  os << deq[0];
  for (ll i = 1; i < deq.size(); ++i) {
    os << ' ' << deq[i];
  }
  return os;
}
template <class T, class L>
ostream& operator<<(ostream& os, pair<T, L>& p) {
  os << p.first << " " << p.second;
  return os;
}
template <class T, class L>
pair<T, L> operator+(pair<T, L> a, pair<T, L> b) {
  return {a.first + b.first, a.second + b.second};
}
template <class Head, class... Tail>
inline void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(move(tail)...);
}
template <class T>
inline bool out(T t) {
  cout << t << '\n';
  return 0;
}
inline bool out() {
  cout << '\n';
  return 0;
}
template <class Head, class... Tail>
inline bool out(Head head, Tail... tail) {
  cout << head << ' ';
  out(move(tail)...);
  return 0;
}
template <class T>
inline void err(T t) {
  cerr << t << '\n';
}
inline void err() { cerr << '\n'; }
template <class Head, class... Tail>
inline void err(Head head, Tail... tail) {
  cerr << head << ' ';
  out(move(tail)...);
}
signed main() {
  ll h, w, k;
  in(h, w, k);
  vector<string> s(h);
  in(s);
  ll closed = 0;
  for (auto&& i : s)
    for (auto&& j : i)
      if (j == '#') closed++;
  vector<vector<vector<ll>>> cost(h,
                                  vector<vector<ll>>(w, vector<ll>(500, LINF)));
  ll ans = 0;
  queue<tuplis> q, q2, q3;
  for (ll i = 0; i < h; ++i)
    for (ll j = 0; j < w; ++j)
      if (s[i][j] == 'S') {
        cost[i][j][0] = 0;
        q3.push({0, {i, j}});
      }
  while (true) {
    if (q.empty() && q2.empty()) {
      q.swap(q3);
      ans++;
    }
    ll x, y, b;
    if (q2.empty() ||
        (!q.empty() && cost[q.front().second.first][q.front().second.second]
                           [q2.front().first] <=
                           cost[q2.front().second.first]
                               [q2.front().second.second][q2.front().first])) {
      x = q.front().second.first;
      y = q.front().second.second;
      b = q.front().first;
      q.pop();
    } else {
      x = q2.front().second.first;
      y = q2.front().second.second;
      b = q2.front().first;
      q2.pop();
    }
    if (b == 399) continue;
    if (x == 0 || y == 0 || x == h - 1 || y == w - 1) {
      return out(ans);
    }
    for (auto&& i : four)
      if (update_min(cost[x + i.first][y + i.second]
                         [b + (s[x + i.first][y + i.second] == '#')],
                     cost[x][y][b] + 1)) {
        if (b + (s[x + i.first][y + i.second] == '#') > k * (ans - 1) ||
            cost[x][y][b] + 1 > k * ans)
          q3.push({b + (s[x + i.first][y + i.second] == '#'),
                   {x + i.first, y + i.second}});
        else
          q2.push({b + (s[x + i.first][y + i.second] == '#'),
                   {x + i.first, y + i.second}});
      }
  }
}
