#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int nearist(int i, int j, int h, int w) { return min({i, j, h - i, w - j}); }
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  if (0) printf("*debug mode*\n");
  int h, w, k;
  cin >> h >> w >> k;
  string maze[h];
  list<pair<int, int>> que;
  for (int i = 0; i < h; i++) {
    cin >> maze[i];
    for (int j = 0; j < w; j++) {
      if (maze[i].at(j) == 'S') que.push_back(make_pair(i, j));
    }
  }
  int near = nearist(que.front().first, que.front().second, h, w);
  for (int i = 0; i < k; i++) {
    list<pair<int, int>> temp;
    for (auto const &cell : que) {
      for (i = 0; i < 4; i++) {
        int ii = cell.first + dx[i], jj = cell.second + dy[i];
        if (not(0 <= ii && ii < h && 0 <= jj && jj <= w)) continue;
        if (maze[ii].at(jj) == '#') continue;
        temp.push_back(make_pair(ii, jj));
        chmin(near, nearist(ii, jj, h, w));
      }
    }
    que.swap(temp);
  }
  cout << 1 + (near + k - 1) / k;
}
