#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const long long inf = 1000000007LL;
const long long maxn = 805LL;
const long long mod = 1000000007LL;
int n, m, k;
int sx, sy;
bool g[maxn][maxn];
void input() {
  cin >> n >> m >> k;
  for (int(i) = (1); (i) <= (n); i++)
    for (int(j) = (1); (j) <= (m); j++) {
      char c;
      cin >> c;
      g[i][j] = (c != '#');
      if (c == 'S') {
        sx = i;
        sy = j;
      }
    }
}
int ans;
bool vis[maxn][maxn];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
void BFS() {
  queue<pair<int, int> > q;
  ans = inf;
  q.push(make_pair(sx, sy));
  vis[sx][sy] = true;
  while (!q.empty()) {
    int x = q.front().first, y = q.front().second;
    q.pop();
    int tmp = min(min(n - x, x - 1), min(m - y, y - 1));
    if (x == 0 || y == 0 || x == n || y == m) {
      ans = 1;
      break;
    }
    ans = min(ans, (tmp + k - 1) / k);
    for (int(i) = (0); (i) <= (3); i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx <= 0 || nx > n || ny <= 0 || ny > m) continue;
      if (vis[nx][ny] || !g[nx][ny]) continue;
      vis[nx][ny] = true;
      q.push(make_pair(nx, ny));
    }
  }
}
int main() {
  input();
  BFS();
  cout << ans + 1 << endl;
  return 0;
}
