#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
double fRand(double fMin, double fMax) {
  double f = (double)rand() / RAND_MAX;
  return fMin + f * (fMax - fMin);
}
template <class T>
T min(T a, T b, T c) {
  return min(a, min(b, c));
}
template <class T>
T max(T a, T b, T c) {
  return max(a, max(b, c));
}
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int w, h, k, xS, yS, d[1005][1005];
char s[1005][1005];
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int x = (0); x <= (h - 1); ++x) scanf("%s", s[x]);
  for (int x = (0); x <= (h - 1); ++x)
    for (int y = (0); y <= (w - 1); ++y) {
      if (s[x][y] == 'S') {
        xS = x;
        yS = y;
      }
    }
  deque<pair<int, int> > q;
  d[xS][yS] = 1;
  q.push_back(make_pair(xS, yS));
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    for (int i = (0); i <= (3); ++i) {
      int u = x + dx[i], v = y + dy[i];
      if (u >= 0 && u < h && v >= 0 && v < w && s[u][v] != '#' && !d[u][v]) {
        d[u][v] = d[x][y] + 1;
        q.push_back(make_pair(u, v));
      }
    }
  }
  for (int x = (0); x <= (h - 1); ++x)
    for (int y = (0); y <= (w - 1); ++y) {
      if (d[x][y] > 0 && d[x][y] <= k + 1) {
        q.push_back(make_pair(x, y));
        d[x][y] = 0;
      } else
        d[x][y] = 1000000007;
    }
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    for (int i = (0); i <= (3); ++i) {
      int u = x + dx[i], v = y + dy[i];
      if (u >= 0 && u < h && v >= 0 && v < w) {
        int c = (s[u][v] == '#');
        if (d[u][v] > d[x][y] + c) {
          d[u][v] = d[x][y] + c;
          if (c == 1)
            q.push_back(make_pair(u, v));
          else
            q.push_front(make_pair(u, v));
        }
      }
    }
  }
  int ans = 1000000007;
  for (int x = (0); x <= (h - 1); ++x)
    for (int y = (0); y <= (w - 1); ++y) {
      if (x == 0 || x == h - 1 || y == 0 || y == w - 1)
        ans = min(ans, (d[x][y] + k - 1) / k + 1);
    }
  printf("%d", ans);
  return 0;
}
