#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
int main() {
  int h, w, k;
  char a[1010][1010];
  int sx, sy;
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 0; i < h; i++) {
    scanf("%s", a[i]);
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  queue<pair<int, int> > que;
  bool used[1010][1010] = {};
  que.push({sy, sx});
  for (int i = 0; i < k; i++) {
    queue<pair<int, int> > que2;
    while (que.size()) {
      int y = que.front().first;
      int x = que.front().second;
      que.pop();
      if (used[y][x]) continue;
      used[y][x] = 1;
      for (int i = 0; i < 4; i++) {
        if (0 <= dx[i] + x && dx[i] + x < w && 0 <= dy[i] + y &&
            dy[i] + y < h) {
          if (a[dy[i] + y][dx[i] + x] == '.') {
            que2.push({dy[i] + y, dx[i] + x});
          }
        }
      }
    }
    que = que2;
  }
  bool used2[1010][1010] = {};
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (used[i][j]) que.push({i, j});
    }
  }
  que.push({sy, sx});
  for (int i = 0; que.size(); i++) {
    queue<pair<int, int> > que2;
    while (que.size()) {
      int y = que.front().first;
      int x = que.front().second;
      que.pop();
      if (used2[y][x]) continue;
      used2[y][x] = 1;
      if (x == 0 || x == w - 1 || y == 0 || x == h - 1) {
        printf("%d\n", (i + k - 1) / k + 1);
        return 0;
      }
      for (int i = 0; i < 4; i++) {
        if (0 <= dx[i] + x && dx[i] + x < w && 0 <= dy[i] + y &&
            dy[i] + y < h) {
          que2.push({dy[i] + y, dx[i] + x});
        }
      }
    }
    que = que2;
  }
  puts("-1");
  return 0;
}
