#include <bits/stdc++.h>
using namespace std;
int H, W, K;
bool A[803][803];
int d[803][803];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  cin >> H >> W >> K;
  for (int(i) = 0; (i) < (int)(H + 2); (i)++) {
    for (int(j) = 0; (j) < (int)(W + 2); (j)++) {
      A[i][j] = false;
      d[i][j] = 1000000;
    }
  }
  queue<pair<int, int> > que;
  for (int(i) = 0; (i) < (int)(H); (i)++) {
    string s;
    cin >> s;
    for (int(j) = 0; (j) < (int)(W); (j)++) {
      if (s[j] == '#') {
        A[i + 1][j + 1] = false;
      } else {
        A[i + 1][j + 1] = true;
      }
      if (s[j] == 'S') {
        d[i + 1][j + 1] = 0;
        que.push(pair<int, int>(i + 1, j + 1));
      }
    }
  }
  while (!que.empty()) {
    int x, y;
    tie(x, y) = que.front();
    que.pop();
    if (d[x][y] == K) continue;
    for (int(i) = 0; (i) < (int)(4); (i)++) {
      int xx = x + dx[i];
      int yy = y + dy[i];
      if (A[xx][yy] == true) {
        if (d[xx][yy] == 1000000) {
          d[xx][yy] = d[x][y] + 1;
        }
      }
    }
  }
  int ans = 1000000000;
  for (int(i) = 0; (i) < (int)(H + 2); (i)++) {
    for (int(j) = 0; (j) < (int)(W + 2); (j)++) {
      if (d[i][j] < K + 1) {
        ans = min(ans, (i - 1 + K - 1) / K);
        ans = min(ans, (H - i + K - 1) / K);
        ans = min(ans, (j - 1 + K - 1) / K);
        ans = min(ans, (W - j + K - 1) / K);
      }
    }
  }
  cout << ans + 1 << endl;
  return 0;
}
