#include <bits/stdc++.h>
using namespace std;
const int maxn = 805;
int n, m, K;
string s[maxn];
bool vis[maxn][maxn];
int d[maxn][maxn];
int ret;
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int cal(int x, int y, int tx, int ty) {
  return (abs(tx - x) + abs(ty - y) + K - 1) / K;
}
int main() {
  cin >> n >> m >> K;
  for (int i = 0; i < n; i++) cin >> s[i];
  ret = 1000000007;
  int sx, sy;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (s[i][j] == 'S') {
        sx = i, sy = j;
      }
  queue<int> qx, qy;
  qx.push(sx);
  qy.push(sy);
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    vis[x][y] = 1;
    ret = min(ret, cal(x, y, x, 0) + 1);
    ret = min(ret, cal(x, y, x, m - 1) + 1);
    ret = min(ret, cal(x, y, 0, y) + 1);
    ret = min(ret, cal(x, y, n - 1, y) + 1);
    for (int i = 0; i < 4; i++) {
      int nx = x + dir[i][0];
      int ny = y + dir[i][1];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || vis[nx][ny] ||
          s[nx][ny] == '#')
        continue;
      d[nx][ny] = d[x][y] + 1;
      if (d[nx][ny] <= K) {
        qx.push(nx);
        qy.push(ny);
      }
    }
  }
  cout << ret << endl;
  return 0;
}
