#include <bits/stdc++.h>
using namespace std;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int h, w, k;
void bfs(vector<string> &a, int y, int x, int k, set<pair<int, int> > &vis) {
  vis.insert(make_pair(y, x));
  if (k) {
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (vis.find(make_pair(ny, nx)) == vis.end() && 0 <= ny && ny < h &&
          0 <= nx && nx < w && a[ny][nx] != '#') {
        bfs(a, ny, nx, k - 1, vis);
      }
    }
  }
}
int main() {
  cin >> h >> w >> k;
  vector<string> a(h);
  int sy, sx;
  for (int i = 0; i < h; i++) {
    cin >> a[i];
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') sy = i, sx = j;
    }
  }
  if (sy % (h - 1) == 0 || sx % (w - 1) == 0) {
    cout << 0 << endl;
    return 0;
  }
  set<pair<int, int> > vis_k;
  bfs(a, sy, sx, k, vis_k);
  int res = 1e9;
  for (auto p : vis_k) {
    res = min(res, (p.first + k - 1) / k);
    res = min(res, (p.second + k - 1) / k);
    res = min(res, (int)(h - p.first - 1 + k - 1) / k);
    res = min(res, (int)(w - p.second - 1 + k - 1) / k);
  }
  cout << res + 1 << endl;
  return 0;
}
