#include <bits/stdc++.h>
using namespace std;
long long qp(int a, long long b) {
  int ans = 1;
  do {
    if (b & 1) ans = 1ll * ans * a;
    a = 1ll * a * a;
  } while (b >>= 1);
  return ans;
}
long long qp(int a, long long b, int mo) {
  int ans = 1;
  do {
    if (b & 1) ans = 1ll * ans * a % mo;
    a = 1ll * a * a % mo;
  } while (b >>= 1);
  return ans;
}
long long gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
long long INF = 1e19;
int c;
int h, w, k;
char grid[801][801];
int used[801][801];
int sx, sy;
int ax, ay;
int wall(int x, int y) { return min(min(h - x - 1, x), min(w - y - 1, y)); }
int mind = 10000000;
void dfs(int x, int y, int n) {
  c++;
  used[x][y] = 1;
  if (n <= k) {
    if (wall(x, y) < mind) {
      mind = wall(x, y);
      ax = x, ay = y;
    }
  } else
    return;
  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 && grid[nx][ny] != '#' &&
        used[nx][ny] != 1) {
      dfs(nx, ny, n + 1);
    }
  }
  return;
}
int main() {
  cin >> h >> w >> k;
  for (int(i) = 0; (i) < (h); (i)++)
    for (int(j) = 0; (j) < (w); (j)++) {
      cin >> grid[i][j];
      if (grid[i][j] == 'S') {
        sx = i, sy = j;
      }
    }
  if (sx <= k) {
    for (int i = 0; i <= sx; i++) {
      if (grid[sx - i][sy] == '#') goto B;
    }
    cout << 1 << endl;
    return 0;
  }
  if (h - 1 - sx <= k) {
    for (int i = 0; i < h - sx; i++) {
      if (grid[sx + i][sy] == '#') goto B;
    }
    cout << 1 << endl;
    return 0;
  }
  if (sy <= k) {
    for (int i = 0; i <= sy; i++) {
      if (grid[sx][sy - i] == '#') goto B;
    }
    cout << 1 << endl;
    return 0;
  }
  if (w - 1 - sy <= k) {
    for (int i = 0; i < w - sy; i++) {
      if (grid[sx][sy + i] == '#') goto B;
    }
    cout << 1 << endl;
    return 0;
  }
B:;
  ax = sx, ay = sy;
  dfs(sx, sy, 0);
  if (mind == 0)
    cout << 1 << endl;
  else
    cout << 2 + (mind - 1) / k << endl;
  return 0;
}
