#include <bits/stdc++.h>
using namespace std;
int ans = 0;
vector<string> mp;
vector<vector<int>> b;
int check(int x, int y, int k, int w, int h) {
  return ceil(double(min(min(abs(x - w) - 1, x), min(abs(y - h) - 1, y))) / k) +
         1;
}
void DFS(int x, int y, int k, int w, int h, int kk) {
  if (x == 0 || x == w - 1 || y == 0 || y == h - 1) {
    ans = 1;
    return;
  }
  if (b[y][x] > k) {
    return;
  }
  b[y][x] = k;
  if (ans > check(x, y, kk, w, h)) {
    ans = check(x, y, kk, w, h);
  }
  if (k < 0) {
    return;
  }
  if (mp[y][x - 1] == '.') {
    if (x - 1 <= 0) {
      ans = 1;
      return;
    }
    DFS(x - 1, y, k - 1, w, h, kk);
  }
  if (mp[y][x + 1] == '.') {
    if (x + 1 >= w - 1) {
      ans = 1;
      return;
    }
    DFS(x + 1, y, k - 1, w, h, kk);
  }
  if (mp[y - 1][x] == '.') {
    if (y - 1 <= 0) {
      ans = 1;
      return;
    }
    DFS(x, y - 1, k - 1, w, h, kk);
  }
  if (mp[y + 1][x] == '.') {
    if (y + 1 >= h - 1) {
      ans = 1;
      return;
    }
    DFS(x, y + 1, k - 1, w, h, kk);
  }
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  mp.resize(h);
  b.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> mp[i];
    b[i].resize(w);
    for (int j = 0; j < w; j++) b[i][j] = 0;
  }
  int x, y;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (mp[i][j] == 'S') {
        x = j;
        y = i;
      }
  ans = w * h;
  DFS(x, y, k, w, h, k);
  cout << ans << endl;
  return 0;
}
