#include <bits/stdc++.h>
using namespace std;
const int N = 809;
const int Tx[] = {-1, 0, 1, 0};
const int Ty[] = {0, 1, 0, -1};
char a[N][N];
bool vis[N][N], h[N][N];
int H, W, k;
bool ck(int x, int y) {
  return (x >= 1 && y >= 1 && x <= H && y <= W && a[x][y] == '.' &&
          h[x][y] <= k && vis[x][y] == false);
}
int getans(int x, int y) {
  int res = min(min(H - x, x - 1), min(W - y, y - 1));
  if (res % k == 0)
    return res / k;
  else
    return res / k + 1;
}
queue<pair<int, int> > q;
int bfs(int x, int y) {
  int res = H * W;
  q.push({x, y});
  vis[x][y] = true;
  while (q.size() > 0) {
    int a = q.front().first, b = q.front().second;
    vis[a][b] = true;
    res = min(res, getans(a, b) + 1);
    q.pop();
    for (int i = 0; i < 4; i++) {
      int na = a + Tx[i], nb = b + Ty[i];
      if (ck(na, nb) == true) {
        q.push({na, nb});
        h[na][nb] = h[a][b] + 1;
        vis[na][nb] = true;
      }
    }
  }
  return res;
}
int main() {
  int x = 1, y = 1;
  cin >> H >> W >> k;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') x = i, y = j;
    }
  }
  cout << bfs(x, y);
}
