#include <bits/stdc++.h>
using namespace std;
template <class T>
bool minn(T &a, T b) {
  return (a > b ? (a = b, 1) : 0);
}
template <class T>
bool maxn(T &a, T b) {
  return (a < b ? (a = b, 1) : 0);
}
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int dis[805][805], cur;
string f[805];
int nceil(int a, int b) { return (a + b - 1) / b; }
int main(void) {
  int N, M, K;
  cin >> N >> M >> K;
  int sx = -1, sy = -1;
  for (int i = 0; i < N; i++) {
    cin >> f[i];
    for (int(j) = 0; j < (int)(M); j++)
      if (f[i][j] == 'S') {
        sx = i;
        sy = j;
      }
  }
  vector<pair<int, int> > q;
  for (int(i) = 0; i < (int)(N); i++)
    for (int(j) = 0; j < (int)(M); j++) dis[i][j] = 1e9;
  dis[sx][sy] = 0;
  q.push_back(make_pair(sx, sy));
  for (int(cue) = 0; cue < (int)(q.size()); cue++) {
    int x = q[cur].first, y = q[cur].second;
    int ncur = dis[x][y];
    for (int(d) = 0; d < (int)(4); d++) {
      int xx = x + dx[d], yy = y + dy[d];
      if (min(xx, yy) < 0 || min(N - xx, M - yy) <= 0 || dis[xx][yy] < 1e8 ||
          f[xx][yy] == '#')
        continue;
      dis[xx][yy] = ncur + 1;
      q.push_back(make_pair(xx, yy));
    }
  }
  int ans = 1e9;
  for (int(i) = 0; i < (int)(N); i++)
    for (int(j) = 0; j < (int)(M); j++) {
      if (dis[i][j] > K) continue;
      int res = 1e9;
      minn(res, nceil(i, K));
      minn(res, nceil(j, K));
      minn(res, nceil(N - i - 1, K));
      minn(res, nceil(M - j - 1, K));
      minn(ans, res + 1);
    }
  cout << ans << endl;
  return 0;
}
