#include <bits/stdc++.h>
using namespace std;
template <class T>
bool minn(T &a, T b) {
  return (a > b ? (a = b, 1) : 0);
}
template <class T>
bool maxn(T &a, T b) {
  return (a < b ? (a = b, 1) : 0);
}
struct node {
  int a, b;
  node() {}
  node(int e, int f) {
    a = e;
    b = f;
  }
};
int ce(int a, int b) { return (a % b == 0 ? a / b : a / b + 1); }
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
vector<node> q;
char c[805][805];
int dist[805][805];
int main(void) {
  int N, M, K;
  memset(dist, 0x7f, sizeof(dist));
  cin >> N >> M >> K;
  for (int(i) = 0; i < (int)(N); i++) cin >> c[i];
  int X, Y;
  for (int(i) = 0; i < (int)(N); i++)
    for (int(j) = 0; j < (int)(M); j++)
      if (c[i][j] == 'S') X = i, Y = j;
  dist[X][Y] = 0;
  q.push_back(node(X, Y));
  for (int(cur) = 0; cur < (int)(q.size()); cur++) {
    int x = q[cur].a;
    int y = q[cur].b;
    int z = dist[x][y];
    for (int(d) = 0; d < (int)(4); d++) {
      int xn = x + dx[d];
      int yn = y + dy[d];
      if (min(xn, yn) < 0 || min(N - xn, M - yn) <= 0 || dist[xn][yn] < 0x7f ||
          c[xn][yn] == '#')
        continue;
      dist[xn][yn] = z + 1;
      q.push_back(node(xn, yn));
    }
  }
  int ans = 0x7f;
  for (int(i) = 0; i < (int)(N); i++)
    for (int(j) = 0; j < (int)(M); j++) {
      if (dist[i][j] > K) continue;
      int res = 0x7f;
      minn(res, ce(i, K));
      minn(res, ce(j, K));
      minn(res, ce(N - i - 1, K));
      minn(res, ce(M - j - 1, K));
      minn(ans, res + 1);
    }
  cout << ans << endl;
  return 0;
}
