#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
using namespace std;
long long readInt() {
  bool minus1 = false;
  long long result = 0;
  char ch;
  ch = getchar();
  while (true) {
    if (ch == '-') break;
    if (ch >= '0' && ch <= '9') break;
    ch = getchar();
  }
  if (ch == '-')
    minus1 = true;
  else
    result = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9') break;
    result = result * 10 + (ch - '0');
  }
  if (minus1)
    return -result;
  else
    return result;
}
int n, m, K;
int go(pair<int, int> L) {
  int x = L.first;
  int y = L.second;
  int mn = x;
  mn = min(mn, y);
  mn = min(mn, n - 1 - x);
  mn = min(mn, m - 1 - y);
  return (mn + K - 1) / K;
}
char a[888][888];
int dx[] = {+1, -1, 0, 0};
int dy[] = {0, 0, +1, -1};
int d[888][888];
int used[888][888];
int main() {
  n = readInt(), m = readInt(), K = readInt();
  pair<int, int> s;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        s = {i, j};
      }
      d[i][j] = MOD;
    }
  }
  d[s.first][s.second] = 0;
  queue<pair<int, int> > Q;
  Q.push(s);
  int ans = go(s) + 1;
  while (!Q.empty()) {
    pair<int, int> cur = Q.front();
    Q.pop();
    for (int i : {0, 1, 2, 3}) {
      int new_x = cur.first + dx[i];
      int new_y = cur.second + dy[i];
      if (new_x <= 0 || new_y <= 0 || new_x > n || new_y > m) continue;
      if (a[new_x][new_y] == '#') {
        continue;
      }
      if (d[new_x][new_y] != MOD) {
        continue;
      }
      d[new_x][new_y] = d[cur.first][cur.second] + 1;
      ans = min(ans, go({new_x, new_y}));
      if (d[new_x][new_y] == K) continue;
      Q.push(pair<int, int>(new_x, new_y));
    }
  }
  cout << ans + 1;
  return 0;
}
