#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 810;
const int inf = INT_MAX / 10;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int h, w, k;
string g[maxn];
int sx, sy;
int dist[810][810];
queue<pair<int, int>> q;
bool onedge(int x, int y) {
  return x == 0 || x == h - 1 || y == 0 || y == w - 1;
}
bool inner(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }
void dfs(int x, int y, int d) {
  dist[x][y] = 0;
  q.emplace(x, y);
  for (int(i) = 0; (i) < (4); (i)++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (inner(nx, ny) && g[nx][ny] == '.' && dist[nx][ny] == inf && d < k) {
      dfs(nx, ny, d + 1);
    }
  }
}
void bfs() {
  while (q.size()) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    for (int(i) = 0; (i) < (4); (i)++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (inner(nx, ny) && dist[nx][ny] == inf) {
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace(nx, ny);
      }
    }
  }
}
int main(void) {
  cin >> h >> w >> k;
  for (int(i) = 0; (i) < (h); (i)++) {
    cin >> g[i];
    for (int(j) = 0; (j) < (w); (j)++)
      if (g[i][j] == 'S') sx = i, sy = j;
  }
  for (int(i) = 0; (i) < (h); (i)++)
    for (int(j) = 0; (j) < (w); (j)++)
      if (!(i == sx && j == sy)) dist[i][j] = inf;
  dfs(sx, sy, 0);
  for (int(i) = 0; (i) < (h); (i)++)
    for (int(j) = 0; (j) < (w); (j)++) {
      if (onedge(i, j) && dist[i][j] == 0) {
        cout << 1 << '\n';
        return 0;
      }
    }
  bfs();
  int ans = inf;
  for (int(i) = 0; (i) < (h); (i)++)
    for (int(j) = 0; (j) < (w); (j)++) {
      if (onedge(i, j)) {
        ans = min(ans, dist[i][j]);
      }
    }
  cout << 1 + (ans + k - 1) / k << '\n';
}
