#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, m, k, sx, sy, ans = INT_MAX;
bool vis[805][805], b[805][805];
int dist[805][805];
char t;
void swap_int(int &a, int &b) {
  a ^= b;
  b ^= a;
  a ^= b;
}
bool inrange(int x, int y) { return x > 0 && x <= n && y > 0 && y <= m; }
queue<pair<int, int> > q, que;
int main(int argv, char *argc[]) {
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      t = getchar();
      while (t != '#' && t != 'S' && t != '.') {
        t = getchar();
      }
      b[i][j] = (t == '#');
      if (t == 'S') {
        sx = i, sy = j;
      }
    }
  }
  q.push(pair<int, int>(sx, sy));
  vis[sx][sy] = 1;
  while (!q.empty()) {
    pair<int, int> fr = q.front();
    q.pop();
    int x = fr.first, y = fr.second;
    for (int dir = 0; dir < 4; dir++) {
      int nx = x + dx[dir], ny = y + dy[dir];
      if (inrange(nx, ny)) {
        if (!vis[nx][ny] && !b[nx][ny]) {
          vis[nx][ny] = 1;
          dist[nx][ny] = dist[x][y] + 1;
          q.push(pair<int, int>(nx, ny));
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (vis[i][j]) {
        if (dist[i][j] <= k) {
          que.push(pair<int, int>(i, j));
        }
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> fr = que.front();
    que.pop();
    int x = fr.first, y = fr.second;
    for (int dir = 0; dir < 4; dir++) {
      int nx = x + dx[dir], ny = y + dy[dir];
      if (inrange(nx, ny)) {
        if (!vis[nx][ny]) {
          vis[nx][ny] = 1;
          dist[nx][ny] = dist[x][y] + 1;
          que.push(pair<int, int>(nx, ny));
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (vis[i][j] && ((i == n && j == m) || (i == 1 && j == 1))) {
        ans = min(ans, dist[i][j]);
      }
    }
  }
  ans = (ans + k - 1) / k + 1;
  cout << ans << '\n';
  return 0;
}
