#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
queue<pair<int, int> > Q;
int dis[805][805], n, m, k;
bool stt[805][805];
void BFS(int stx, int sty) {
  memset(dis, -1, sizeof(dis));
  int xb;
  dis[stx][sty] = 0;
  Q.push(make_pair(stx, sty));
  while (!Q.empty()) {
    pair<int, int> cur = Q.front();
    Q.pop();
    for (xb = 0; xb < 4; xb++) {
      int c1 = cur.first + dx[xb], c2 = cur.second + dy[xb];
      if (dis[c1][c2] == -1 && stt[c1][c2]) {
        dis[c1][c2] = dis[cur.first][cur.second] + 1;
        Q.push(make_pair(c1, c2));
      }
    }
  }
}
inline char gc() {
  char xx = getchar();
  while (xx == ' ' || xx == '\n') xx = getchar();
  return xx;
}
inline int getp(int x, int y) {
  return ((((((n - 1 - x) < (x)) ? (n - 1 - x) : (x))) <
           ((((m - 1 - y) < (y)) ? (m - 1 - y) : (y))))
              ? ((((n - 1 - x) < (x)) ? (n - 1 - x) : (x)))
              : ((((m - 1 - y) < (y)) ? (m - 1 - y) : (y))));
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  int xb, x2, a, b;
  for (xb = 0; xb < n; xb++) {
    for (x2 = 0; x2 < m; x2++) {
      char xx = gc();
      if (xx == '#')
        stt[xb][x2] = 0;
      else
        stt[xb][x2] = 1;
      if (xx == 'S') {
        a = xb;
        b = x2;
      }
    }
  }
  BFS(a, b);
  int res = 1e9;
  for (xb = 0; xb < n; xb++) {
    for (x2 = 0; x2 < m; x2++) {
      if (dis[xb][x2] >= 0)
        res = (((res) < ((int)((dis[xb][x2] + k - 1) / k) +
                         (int)((getp(xb, x2) + k - 1) / k)))
                   ? (res)
                   : ((int)((dis[xb][x2] + k - 1) / k) +
                      (int)((getp(xb, x2) + k - 1) / k)));
    }
  }
  printf("%d\n", res);
  return 0;
}
