#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 810;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int h, w, k;
int sx, sy;
string g[810];
int dist[MAX_N][MAX_N];
queue<pair<int, int>> q;
int manghattan(int i, int j) { return abs(sx - i) + abs(sy - j); }
void dfs(int x = sx, int y = sy, int d = 0) {
  dist[x][y] = 0;
  for (int(i) = 0; (i) < (4); (i)++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < h && ny >= 0 && ny < w) {
      if (g[nx][ny] == '.' && d < k) {
        dfs(nx, ny, d + 1);
      }
    }
  }
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w >> k;
  for (int(i) = 0; (i) < (h); (i)++) {
    string s;
    cin >> s;
    g[i] = s;
  }
  for (int(i) = 0; (i) < (h); (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] = INT_MAX / 10;
  dfs();
  for (int(i) = 0; (i) < (h); (i)++)
    for (int(j) = 0; (j) < (w); (j)++)
      if (i == 0 || i == h - 1 || j == 0 || j == w - 1) {
        if (dist[i][j] == 0) {
          cout << 1 << '\n';
          return 0;
        }
      }
  for (int(i) = 0; (i) < (h); (i)++)
    for (int(j) = 0; (j) < (w); (j)++)
      if (dist[i][j] == 0) q.emplace(i, j);
  int x, y, nx, ny;
  while (q.size()) {
    tie(x, y) = q.front();
    q.pop();
    for (int(d) = 0; (d) < (4); (d)++) {
      nx = x + dx[d];
      ny = y + dy[d];
      auto inner = [](int x, int y) -> bool {
        return x >= 0 && x <= h - 1 && y >= 0 && y <= w - 1;
      };
      auto edge = [](int x, int y) -> bool {
        return (x == 0 || x == h - 1 || y == 0 || y == w - 1);
      };
      if (!inner(nx, ny)) continue;
      if (edge(nx, ny)) {
        cout << 1 + (dist[x][y] + k) / k << '\n';
        return 0;
      } else {
        if (dist[x][y] + 1 < dist[nx][ny]) {
          dist[nx][ny] = dist[x][y] + 1;
          q.emplace(nx, ny);
        }
      }
    }
  }
}
