#include <bits/stdc++.h>
using namespace std;
char s[808][808];
int vis[808][808];
int n, m, k;
int minstep(int a, int b) {
  int ans = min(ans, a / k + (a % k > 0));
  ans = min(ans, b / k + (b % k > 0));
  ans = min(ans, (n - a - 1) / k + ((n - a - 1) % k > 0));
  ans = min(ans, (m - b - 1) / k + ((m - b - 1) % k > 0));
  return ans;
}
struct num {
  int x, y, val;
};
bool operator<(num a, num b) {
  if (a.val != b.val) return a.val < b.val;
  if (a.x != b.x) return a.x < b.x;
  return a.y < b.y;
}
bool pan(int a, int b) { return a >= 0 && b >= 0 && a < n && b < m; }
int main() {
  int a, b, c, d, e, f, g, h;
  ios::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') {
        e = i;
        f = j;
      }
    }
  }
  memset(vis, -1, sizeof(vis));
  priority_queue<num> que;
  num tmp, tmp1;
  vis[e][f] = 0;
  tmp.x = e, tmp.y = f;
  tmp.val = 0;
  int ans = min(n, m) + 1;
  que.push(tmp);
  int step[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
  while (!que.empty()) {
    tmp = que.top();
    que.pop();
    e = tmp.x, f = tmp.y;
    ans = min(ans, 1 + minstep(e, f));
    if (vis[e][f] == k) continue;
    for (int i = 0; i < 4; i++) {
      g = e + step[i][0], h = f + step[i][1];
      if (pan(g, h) && s[g][h] != '#' && vis[g][h] == -1) {
        vis[g][h] = vis[e][f] + 1;
        tmp1.val = vis[g][h];
        tmp1.x = g, tmp1.y = h;
        que.push(tmp1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
