#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const int Mod = 1000000007;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char s[1010][1010];
bool vis[1010][1010];
queue<pair<int, pair<int, int> > > Q;
int Ceil(int x, int y) {
  if (x % y == 0)
    return x / y;
  else
    return x / y + 1;
}
int main() {
  int n, m, K;
  scanf("%d %d %d", &n, &m, &K);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &s[i][j]);
      if (s[i][j] == 'S') Q.push(make_pair(0, make_pair(i, j)));
    }
  }
  int res = INF;
  while (!Q.empty()) {
    int x = Q.front().second.first, y = Q.front().second.second,
        k = Q.front().first;
    Q.pop();
    if (x == 1 || x == n || y == 1 || y == m) res = 1;
    res = min(res, Ceil(min(min(n - x, x - 1), min(m - y, y - 1)), K) + 1);
    if (x == K) continue;
    for (int i = 0; i < 4; ++i) {
      int tx = x + dx[i], ty = y + dy[i];
      if (!tx || !ty || tx > n || ty > m || s[tx][ty] == '#' || vis[tx][ty])
        continue;
      vis[tx][ty] = true, Q.push(make_pair(k + 1, make_pair(tx, ty)));
    }
  }
  printf("%d\n", res);
  return 0;
}
