#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
  for (auto x : v) os << x << ' ';
  return os;
}
void debug() { cerr << '\n'; }
template <typename H, typename... T>
void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b)) cerr << ", ";
  debug(b...);
}
int h, w, k;
char a[800][800];
bool used[800][800];
bool ok(int x, int y) { return 0 <= x and x < h and 0 <= y and y < w; }
int dist[800][800];
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, 1, -1, 0};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  queue<pii> q;
  for (int i = 0; i < (int)(h); ++i)
    for (int j = 0; j < (int)(w); ++j) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        q.emplace(i, j);
        used[i][j] = true;
        dist[i][j] = k;
      }
    }
  while (q.size()) {
    auto p = q.front();
    q.pop();
    int x = p.first;
    int y = p.second;
    if (dist[x][y] == 0) break;
    for (int i = 0; i < (int)(4); ++i) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (!ok(xx, yy)) {
        cout << 1 << endl;
        return 0;
      }
      if (used[xx][yy] or a[xx][yy] == '#') continue;
      dist[xx][yy] = dist[x][y] - 1;
      used[xx][y] = true;
    }
  }
  int ans = 1 << 30;
  for (int i = 0; i < (int)(h); ++i)
    for (int j = 0; j < (int)(w); ++j) {
      if (used[i][j]) {
        ans = min({ans, (i + k - 1) / k, (h - 1 - i + k - 1) / k,
                   (j + k - 1) / k, (w - 1 - j + k - 1) / k});
      }
    }
  cout << ans + 1 << endl;
}
