#include <bits/stdc++.h>
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6;
const long long LINF = 1e18;
using namespace std;
long long n, cnt = 0, ans = LINF, a, b, c, d[1000][1000], cmp, cmpp, m, h, w, x,
             y, sum = 0, pos, k, sy, sx, gx = 1000, gy = 1000, ri, le, up, down;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
string alph("abcdefghijklmnopqrstuvwxyz");
char s[1000][1000];
bool fl;
struct edge {
  int to, cost;
};
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  for (long long(i) = 0; (i) < (int)(h); (i)++) {
    for (long long(j) = 0; (j) < (int)(w); (j)++) {
      cin >> s[i][j];
      if (s[i][j] == 'S') sy = i, sx = j;
    }
  }
  int min_cost[55][55];
  for (long long(i) = 0; (i) < (int)(h); (i)++)
    for (long long(j) = 0; (j) < (int)(w); (j)++) min_cost[i][j] = INF;
  queue<pair<long long, long long>> qu;
  qu.push({sy, sx});
  min_cost[sy][sx] = 0;
  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();
    for (long long(i) = 0; (i) < (int)(4); (i)++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
      if (s[nx][ny] == '#') continue;
      if (min_cost[p.first][p.second] + 1 >= min_cost[nx][ny]) continue;
      min_cost[nx][ny] = min_cost[p.first][p.second] + 1;
      qu.push({nx, ny});
    }
  }
  for (long long(i) = 0; (i) < (int)(h); (i)++) {
    for (long long(j) = 0; (j) < (int)(w); (j)++) {
      if (min_cost[i][j] <= k) {
        ri = (w - 1 - j) / k;
        if ((w - 1 - j) % k != 0) ri++;
        le = j / k;
        if (j % k != 0) le++;
        up = i / k;
        if (i % k != 0) up++;
        down = (h - 1 - i) / k;
        if ((h - 1 - i) % k != 0) down++;
        if (min_cost[i][j] == 0) {
          if (min_cost[i][j + 1] == INF) ri++;
          if (min_cost[i][j - 1] == INF) le++;
          if (min_cost[i + 1][j] == INF) up++;
          if (min_cost[i - 1][j] == INF) down++;
          cnt = min({ri, le, up, down});
          ans = min(ans, cnt);
        } else {
          cnt = min({ri, le, up, down});
          ans = min(ans, cnt + 1);
        }
      }
    }
  }
  cout << (ans) << endl;
  return 0;
}
