#include <bits/stdc++.h>
using namespace std;
const int MAXHW = 800 + 50;
char a[MAXHW][MAXHW];
int d[MAXHW][MAXHW];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  int H, W, K;
  scanf("%d%d%d", &H, &W, &K);
  for (int i = 0; i < H; i++) scanf("%s", a[i]);
  queue<pair<int, int> > q1;
  queue<pair<int, int> > q2;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++) d[i][j] = -1;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      if (a[i][j] == 'S') {
        d[i][j] = 0;
        q1.push(make_pair(i, j));
      }
  int ans = -1;
  do {
    pair<int, int> u = q1.front();
    int dis = d[u.first][u.second];
    if (dis == K) break;
    q1.pop();
    q2.push(u);
    for (int k = 0; k < 4; k++) {
      int nx = u.first + dx[k];
      int ny = u.second + dy[k];
      if (a[nx][ny] == '.' && (nx == 1 || nx == H || ny == 1 || ny == W)) {
        ans = 1;
      }
      if (d[nx][ny] == -1 && a[nx][ny] == '.') {
        d[nx][ny] = dis + 1;
        q1.push(make_pair(nx, ny));
      }
    }
    if (ans == 1) break;
  } while (0);
  while (!q1.empty()) {
    q2.push(q1.front());
    q1.pop();
  }
  (void)0;
  for (int i = 0; i < ((int)(q2).size()); i++) {
    pair<int, int> u = q2.front();
    q2.pop();
    d[u.first][u.second] = K;
    q2.push(u);
  }
  while (ans == -1) {
    pair<int, int> u = q2.front();
    int dis = d[u.first][u.second];
    q2.pop();
    for (int k = 0; k < 4; k++) {
      int nx = u.first + dx[k];
      int ny = u.second + dy[k];
      if (nx == 1 || nx == H || ny == 1 || ny == W) {
        ans = dis / K + 1;
      }
      if (d[nx][ny] == -1) {
        d[nx][ny] = dis + 1;
        q2.push(make_pair(nx, ny));
      }
    }
  }
  cout << ans << endl;
  return 0;
}
