#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct outputer;
struct outputable {};
template <typename T>
inline auto sqr(T x) -> decltype(x * x) {
  return x * x;
}
template <typename T1, typename T2>
inline bool umx(T1& a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
inline bool umn(T1& a, T2 b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int N = 800;
const int M = 800;
struct Input {
  int n, m, k;
  string a[N];
  bool read() {
    if (!(cin >> n >> m >> k)) {
      return 0;
    }
    getline(cin, a[0]);
    for (int i = int(0); i < int(n); ++i) {
      getline(cin, a[i]);
    }
    return 1;
  }
  void init(const Input& input) { *this = input; }
};
struct Data : Input {
  int ans;
  void write() { cout << ans << endl; }
};
namespace Main {
const int inf = 1000000000;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
struct Solution : Data {
  int num[N][M];
  int d[N][N];
  void solve() {
    vector<pair<int, int> > q;
    for (int i = int(0); i < int(n); ++i) {
      for (int j = int(0); j < int(m); ++j) {
        num[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
        d[i][j] = inf;
        if (a[i][j] == 'S') {
          d[i][j] = 0;
          q.emplace_back(make_pair(i, j));
        }
      }
    }
    ans = inf;
    for (int i = int(0); i < int(((int)(q).size())); ++i) {
      if (d[q[i].first][q[i].second] == 0 &&
          num[q[i].first][q[i].second] == 0) {
        umn(ans, 0);
      } else {
        umn(ans, (max(d[q[i].first][q[i].second], k) +
                  num[q[i].first][q[i].second] + k - 1) /
                     k);
      }
      for (int j = int(0); j < int(4); ++j) {
        int x1 = q[i].first + dx[j];
        int y1 = q[i].second + dy[j];
        if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && a[x1][y1] != '#' &&
            umn(d[x1][y1], d[q[i].first][q[i].second] + 1)) {
          q.emplace_back(make_pair(x1, y1));
        }
      }
    }
  }
  void clear() { *this = Solution(); }
};
}  // namespace Main
Main::Solution sol;
int main() {
  cout.setf(ios::showpoint | ios::fixed);
  cout.precision(20);
  sol.read();
  sol.solve();
  sol.write();
  return 0;
}
