#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
struct Node {
  int x, y;
};
int n, m, k;
char a[810][810];
int dist[810][810];
int la[810][810];
int main() {
  cin >> n >> m >> k;
  queue<Node> qq;
  memset(dist, -1, sizeof(dist));
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') {
        set<pair<int, int> > tss;
        a[i][j] = '.', qq.push(Node{i, j});
      }
    }
  while (!qq.empty()) {
    Node node = qq.front();
    qq.pop();
    dist[node.x][node.y] = k;
    if (node.x < 2 || node.x >= n || node.y < 2 || node.y > m) {
      cout << 1 << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int tx = node.x + dx[i], ty = node.y + dy[i];
      if (tx < 1 || tx > n || ty < 1 || ty > n) continue;
      if (a[tx][ty] == '#') continue;
      if (dist[tx][ty] != -1) continue;
      int tl = la[node.x][node.y] + 1;
      if (tl > k) continue;
      dist[tx][ty] = k;
      la[tx][ty] = la[node.x][node.y] + 1;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (dist[i][j] == k) qq.push(Node{i, j});
  while (!qq.empty()) {
    Node node = qq.front();
    qq.pop();
    if (node.x < 2 || node.x >= n || node.y < 2 || node.y > m) {
      int t = dist[node.x][node.y];
      if (t % k == 0)
        cout << t / k << endl;
      else
        cout << t / k + 1 << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int tx = node.x + dx[i], ty = node.y + dy[i];
      if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
      int ts = dist[node.x][node.y];
      if (a[tx][ty] == '#') ts++;
      if (dist[tx][ty] != -1) continue;
      dist[tx][ty] = dist[node.x][node.y] + 1;
      qq.push(Node{tx, ty});
    }
  }
  cout << -1 << endl;
  return 0;
}
