#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-8;
inline void II(int &n) {
  char ch = getchar();
  n = 0;
  bool t = 0;
  for (; ch < '0'; ch = getchar())
    if (ch == '-') t = 1;
  for (; ch >= '0'; n = n * 10 + ch - '0', ch = getchar())
    ;
  if (t) n = -n;
}
int n, h, w, k;
const int dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char mp[1000][1000];
bool in(int x, int y) {
  if (x >= 1 && x <= h && y >= 1 && y <= w) return 1;
  return 0;
}
bool vis[1000][1000];
int X, Y;
int main() {
  scanf("%d%d", &h, &w);
  scanf(" %d ", &k);
  for (int i = 1; i <= h; ++i) scanf("%s", mp[i] + 1);
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      if (mp[i][j] == 'S') X = i, Y = j;
  queue<pair<int, int> > Q;
  Q.push(make_pair(X, Y));
  vis[X][Y] = 1;
  while (!Q.empty()) {
    pair<int, int> u = Q.front();
    Q.pop();
    int x = u.first, y = u.second;
    for (int i = 0; i <= 3; ++i) {
      int xx = x + dir[i][0], yy = y + dir[i][1];
      if (in(xx, yy) && mp[xx][yy] == '.') {
        if (!vis[xx][yy]) {
          Q.push(make_pair(xx, yy));
          vis[xx][yy] = 1;
        }
      }
    }
  }
  int ans = 0X3F3F3F3F;
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      if (vis[i][j]) {
        int u = i - 1, d = h - i, l = j - 1, r = w - j;
        int s1 = min(u, d), s2 = min(l, r);
        int s = min(s1, s2);
        if (s % k)
          s = s / k + 1;
        else
          s = s / k;
        if (s + 1 < ans) ans = s + 1;
      }
  printf("%d\n", ans);
  return 0;
}
