#include <bits/stdc++.h>
using namespace std;
int compMagicCount(int original, int dist, int k) {
  int mCount = ceil((float)dist / k);
  return original > mCount ? mCount : original;
}
int getDiff(pair<int, int> start, pair<int, int> np) {
  return abs(start.first - np.first) + abs(start.second - np.second);
}
void fds(vector<pair<int, int>> &fPoint, char map[800][800], int h, int w,
         int k) {
  pair<int, int> start = fPoint.at(0);
  int dx[] = {0, 0, 1, -1};
  int dy[] = {1, -1, 0, 0};
  queue<pair<int, int>> que;
  que.push(start);
  bool checker[800][800] = {};
  while (!que.empty()) {
    pair<int, int> p = que.front();
    que.pop();
    int x = p.second;
    int y = p.first;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      pair<int, int> np = make_pair(ny, nx);
      if (0 <= nx && nx < w && 0 <= ny && ny < h && map[ny][nx] == '.' &&
          getDiff(start, np) <= k && !checker[ny][nx]) {
        que.push(np);
        fPoint.push_back(np);
        checker[ny][nx] = true;
      }
    }
  }
}
int main() {
  int h, w, k;
  char map[800][800];
  pair<int, int> start;
  vector<pair<int, int>> firstStep;
  while (cin >> h >> w >> k) {
    firstStep.clear();
    for (int y = 0; y < h; ++y) {
      for (int x = 0; x < w; ++x) {
        char c;
        cin >> c;
        map[y][x] = c;
        if (c == 'S') {
          start = make_pair(y, x);
          firstStep.push_back(start);
        }
      }
    }
    fds(firstStep, map, h, w, k);
    int magicCount = w * h;
    for (pair<int, int> p : firstStep) {
      int y = p.first;
      int x = p.second;
      magicCount = compMagicCount(magicCount, y, k);
      magicCount = compMagicCount(magicCount, h - y, k);
      magicCount = compMagicCount(magicCount, x, k);
      magicCount = compMagicCount(magicCount, w - x, k);
    }
    cout << magicCount + 1 << endl;
  }
}
