#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int N = 810;
char pos[N][N];
int step[N][N];
struct info {
  int x, y, step;
  info operator+(info b) {
    b.x += x;
    b.y += y;
    b.step += step;
    return b;
  }
};
info S, dir[4] = {{1, 0, 1}, {-1, 0, 1}, {0, 1, 1}, {0, -1, 1}};
int n, m, k;
int vis[N][N];
void init() {
  memset(vis, 0, sizeof(vis));
  memset(step, 0, sizeof(step));
}
inline bool check(const info &t) {
  return t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && !vis[t.x][t.y] &&
         pos[t.x][t.y] == '.';
}
void bfs() {
  queue<info> Q;
  Q.push(S);
  step[S.x][S.y] = S.step;
  vis[S.x][S.y] = 1;
  while (!Q.empty()) {
    info u = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      info v = u + dir[i];
      if (check(v)) {
        vis[v.x][v.y] = 1;
        step[v.x][v.y] = v.step;
        Q.push(v);
      }
    }
  }
}
inline int calc(int a, int b) { return a / b + (a % b != 0); }
int main(void) {
  int i, j;
  while (~scanf("%d%d%d", &n, &m, &k)) {
    init();
    for (i = 0; i < n; ++i) {
      scanf("%s", pos[i]);
      for (j = 0; j < m; ++j) {
        if (pos[i][j] == 'S') {
          S.x = i;
          S.y = j;
          S.step = 1;
        }
      }
    }
    bfs();
    int ans = 0x3f3f3f3f;
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j) {
        if (step[i][j]) {
          if (i == 0 || i == n - 1 || j == 0 || j == m - 1)
            ans = min(ans, calc(step[i][j], k));
          else {
            int L = j;
            int R = m - 1 - j;
            int U = i;
            int D = n - 1 - i;
            int base = calc(step[i][j], k);
            ans = min(ans, base + calc(L, k));
            ans = min(ans, base + calc(R, k));
            ans = min(ans, base + calc(U, k));
            ans = min(ans, base + calc(D, k));
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
