#include <bits/stdc++.h>
using namespace std;
const long long BIGER = 1000000000000000;
const int BIG = 1000000000;
int n, m, q;
char a[2001][2001];
int f[2001][2001];
int mn = BIG;
bool shit = false;
int ypa(int x, int y, int k) {
  queue<int> que;
  que.push(x);
  que.push(y);
  que.push(k);
  f[x][y] = 1;
  int t1, t2, t3;
  while (!que.empty()) {
    t1 = que.front();
    que.pop();
    t2 = que.front();
    que.pop();
    t3 = que.front();
    que.pop();
    f[t1][t2] = 1;
    if (t3 == 0) {
      mn = min(mn, min(min(t1 - 1, t2 - 1), min(n - t1, m - t2)));
      continue;
    } else if (t1 >= 1 && t1 <= n && t2 >= 1 && t2 <= m)
      ;
    {
      mn = min(mn, min(min(t1 - 1, t2 - 1), min(n - t1, m - t2)));
      if (a[t1 + 1][t2] == '.' && f[t1 + 1][t2] == 0) {
        que.push(t1 + 1);
        que.push(t2);
        que.push(t3 - 1);
      }
      if (a[t1 - 1][t2] == '.' && f[t1 - 1][t2] == 0) {
        que.push(t1 - 1);
        que.push(t2);
        que.push(t3 - 1);
      }
      if (a[t1][t2 + 1] == '.' && f[t1][t2 + 1] == 0) {
        que.push(t1);
        que.push(t2 + 1);
        que.push(t3 - 1);
      }
      if (a[t1][t2 - 1] == '.' && f[t1][t2 - 1] == 0) {
        que.push(t1);
        que.push(t2 - 1);
        que.push(t3 - 1);
      }
    }
  }
}
int debug() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cerr << a[i][j] << " ";
    }
    cerr << endl;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cerr << f[i][j] << " ";
    }
    cerr << endl;
  }
  cerr << "mn:" << mn << endl;
}
int main() {
  int x, y;
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    scanf("%s", a[i] + 1);
    for (int j = 1; j <= m; j++) {
      if (a[i][j] == 'S') {
        x = i;
        y = j;
        a[i][j] = '.';
      }
    }
  }
  ypa(x, y, q);
  debug();
  if (shit) {
    cout << 1;
    return 0;
  } else {
    cout << 1 + (mn + q - 1) / q;
  }
  return 0;
}
