#include <bits/stdc++.h>
using namespace std;
const int maxn = 805;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char a[maxn][maxn];
int n, m, k, sx, sy, dis[maxn][maxn];
struct node {
  int x, y, obs, fot;
  node() {}
  node(int _, int __, int ___, int ____) {
    x = _;
    y = __;
    obs = ___;
    fot = ____;
  }
  friend bool operator<(node a, node b) {
    return max(ceil(a.obs * 1.0 / (k * 1.0)), ceil(a.fot * 1.0 / (k * 1.0))) >
           max(ceil(b.obs * 1.0 / (k * 1.0)), ceil(b.fot * 1.0 / (k * 1.0)));
  }
} h, ne;
priority_queue<node> q;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; ++i) scanf("%s", a[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (a[i][j] == 'S') h = node(i, j, 0, 0);
  q.push(h);
  memset(dis, 0x3f, sizeof(dis));
  dis[h.x][h.y] = 0;
  while (!q.empty()) {
    h = q.top();
    q.pop();
    for (int i = 0; i <= 3; ++i) {
      ne.x = h.x + dx[i];
      ne.y = h.y + dy[i];
      if (ne.x < 1 || ne.y < 1 || ne.x > n || ne.y > m) continue;
      ne.obs = h.obs + (a[ne.x][ne.y] == '#');
      ne.fot = h.fot + 1;
      if (dis[ne.x][ne.y] >
          max(ceil(ne.obs * 1.0 / (k * 1.0)), ceil(ne.fot * 1.0 / (k * 1.0)))) {
        dis[ne.x][ne.y] =
            max(ceil(ne.obs * 1.0 / (k * 1.0)), ceil(ne.fot * 1.0 / (k * 1.0)));
        q.push(ne);
      }
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 1; i <= n; ++i) ans = min(ans, dis[i][1]);
  for (int i = 1; i <= n; ++i) ans = min(ans, dis[i][m]);
  for (int i = 1; i <= m; ++i) ans = min(ans, dis[1][i]);
  for (int i = 1; i <= m; ++i) ans = min(ans, dis[n][i]);
  printf("%d", ans);
  return 0;
}
