#include <bits/stdc++.h>
using namespace std;
const int N = 803;
int m, n, k, s, t, d[N][N];
char a[N][N];
vector<pair<int, int> > border;
void enter() {
  for (int i = (1); i <= m; i++) {
    string s;
    cin >> s;
    for (int j = (0); j < n; j++) a[i][j + 1] = s[j];
  }
  for (int i = (1); i <= m; i++) {
    for (int j = (1); j <= n; j++) {
      if (a[i][j] == 'S') s = i, t = j;
      if (i == 1 || j == 1 || i == m || j == n) {
        border.push_back(make_pair(i, j));
      }
    }
  }
}
vector<pair<int, int> > cua1;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
void bfs() {
  queue<pair<int, int> > q;
  q.push(make_pair(s, t));
  d[s][t] = 1;
  while (q.size()) {
    int u = q.front().first, v = q.front().second;
    q.pop();
    for (int w = (0); w < 4; w++) {
      int i = u + dx[w], j = v + dy[w];
      if (a[i][j] == '#') continue;
      if (i <= 0 || j <= 0) continue;
      if (i > m || j > n) continue;
      if (d[i][j] == 0) {
        d[i][j] = d[u][v] + 1;
        q.push(make_pair(i, j));
      }
    }
  }
  for (int i = (1); i <= m; i++) {
    for (int j = (1); j <= n; j++) {
      if (d[i][j] > 0 && d[i][j] <= k + 1) {
        cua1.push_back(make_pair(i, j));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> m >> n >> k;
  enter();
  bfs();
  int res = 1e9;
  for (pair<int, int> &e : border) {
    int kc = abs(e.first - s) + abs(e.second - t);
    int ans = -1;
    if ((kc % k) == 0)
      ans = (kc / k) + 1;
    else
      ans = (kc / k) + 2;
    for (pair<int, int> &den : cua1) {
      int u = den.first, v = den.second;
      int dist = abs(e.first - u) + abs(e.second - v);
      if (kc % k == 0) {
        int y = kc / k;
        double x = double(dist) / k;
        --y;
        ans = min(ans, y + 2);
        int xx = int(x);
        if (xx <= y) ans = min(ans, y + 1);
      } else {
        int x = kc / k;
        ++x;
        ans = min(ans, x + 1);
        int y = dist / k;
        if (y < x) ans = min(ans, x);
      }
    }
    res = min(res, ans);
  }
  cout << res;
}
