#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int turn(pair<int, int> a, int k) {
  return max((a.first + k - 1) / k, (a.second + k - 1) / k + (a.second > 0));
}
int compare(pair<int, int> a, pair<int, int> b, int k) {
  int n = turn(a, k), m = turn(b, k);
  if (n < m) {
    return -1;
  } else if (n > m) {
    return 1;
  } else {
    if (a < b) {
      return -1;
    } else if (a > b) {
      return 1;
    } else {
      return 0;
    }
  }
}
int main() {
  int h, w, k, sy, sx;
  bool a[800][800];
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      a[i][j] = s[j] == '#';
      if (s[j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  pair<int, int> p[h][w];
  fill(p[0], p[h], make_pair(100000, 100000));
  p[sy][sx] = make_pair(0, 0);
  deque<pair<int, int>> d;
  d.push_back(make_pair(sy, sx));
  while (!d.empty()) {
    int y = d.front().first, x = d.front().second;
    d.pop_front();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w) {
        continue;
      }
      pair<int, int> q(p[y][x].first + 1, p[y][x].second + (int)a[ny][nx]);
      if (compare(q, p[ny][nx], k) == -1) {
        p[ny][nx] = q;
        d.push_back(make_pair(ny, nx));
      }
    }
  }
  int m = 100000;
  for (int i = 0; i < h; i++) {
    m = min(m, turn(p[i][0], k));
    m = min(m, turn(p[i][w - 1], k));
  }
  for (int i = 0; i < w; i++) {
    m = min(m, turn(p[0][i], k));
    m = min(m, turn(p[h - 1][i], k));
  }
  cout << m << endl;
  return 0;
}
