#include <bits/stdc++.h>
using namespace std;
bool in_room(int i, int j, int h, int w) {
  if (i >= 0 && j >= 0 && i < h && j < w) {
    return true;
  } else
    return false;
}
int min_four(int i, int j, int k, int l) { return min(min(i, j), min(k, l)); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, k;
  cin >> h >> w >> k;
  string room[h];
  for (int i = 0; i < h; i++) cin >> room[i];
  queue<tuple<int, int, int> > q;
  int si, sj;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      if (room[i][j] == 'S') {
        q.push(make_tuple(i, j, 0));
        si = i;
        sj = j;
      }
  }
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  int min_dis = min_four(si, sj, h - 1 - si, w - 1 - sj);
  bool used[h][w];
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) used[i][j] = false;
  while (!q.empty()) {
    int i = get<0>(q.front());
    int j = get<1>(q.front());
    int d = get<2>(q.front());
    q.pop();
    if (used[j][i]) continue;
    used[j][i] = true;
    if (d + 1 > k) continue;
    for (int di = 0; di < 4; di++) {
      if (in_room(i + dx[di], j + dy[di], w, h)) {
        if (room[j + dy[di]][i + dx[di]] == '.') {
          min_dis = min(min_dis,
                        min_four(i + dx[di], j + dy[di], h - 1 - (j + dy[di]),
                                 w - 1 - (i + dx[di])));
          q.push(make_tuple(i + dx[di], j + dy[di], d + 1));
        }
      }
    }
  }
  cout << 1 + (k - 1 + min_dis) / k << endl;
  return 0;
}
