#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());
class CClosedRooms {
 public:
  void solve(istream& cin, ostream& cout) {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int h, w, k;
    cin >> h >> w >> k;
    char mat[h][w];
    vector<vector<int>> cost(h, vector<int>(w, INT_MAX / 2));
    int start = -1;
    deque<P> que;
    for (int i = 0; i < (int)(h); i++)
      for (int j = 0; j < (int)(w); j++) {
        cin >> mat[i][j];
        if (mat[i][j] == 'S') {
          que.emplace_back(i * w + j, 0);
          cost[i][j] = 0;
        }
      }
    while (!que.empty()) {
      auto p = que.front();
      que.pop_front();
      int i = p.first % w;
      int j = p.first / w;
      int c = p.second;
      if (c > cost[i][j]) continue;
      if (i > 0 && mat[i - 1][j] == '.') {
        if (cost[i - 1][j] > c + 1) {
          cost[i - 1][j] = c + 1;
          que.emplace_back((i - 1) * w + j, c + 1);
        }
      }
      if (i < h - 1 && mat[i + 1][j] == '.') {
        if (cost[i + 1][j] > c + 1) {
          cost[i + 1][j] = c + 1;
          que.emplace_back((i + 1) * w + j, c + 1);
        }
      }
      if (j > 0 && mat[i][j - 1] == '.') {
        if (cost[i][j - 1] > c + 1) {
          cost[i][j - 1] = c + 1;
          que.emplace_back(i * w + j - 1, c + 1);
        }
      }
      if (j < w - 1 && mat[i][j + 1] == '.') {
        if (cost[i][j + 1] > c + 1) {
          cost[i][j + 1] = c + 1;
          que.emplace_back(i * w + j + 1, c + 1);
        }
      }
    }
    int distance = INT_MAX;
    for (int i = 0; i < (int)(h); i++)
      for (int j = 0; j < (int)(w); j++) {
        if (cost[i][j] <= k) {
          auto imin = min(i, h - 1 - i);
          auto jmin = min(j, w - 1 - j);
          chmin(distance, min(imin, jmin));
        }
      }
    cout << (distance + k - 1) / k + 1 << endl;
  }
};
signed main() {
  CClosedRooms solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
