#include <bits/stdc++.h>
using namespace std;
int n, m, k, sx, sy;
char M[805][805];
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
queue<pair<int, int> > q;
queue<int> num;
int way(int x, int y) {
  int a1 = ((n - x) + (k - 1)) / k;
  int a2 = ((x) + (k - 1)) / k;
  int a3 = ((m - y) + (k - 1)) / k;
  int a4 = ((y) + (k - 1)) / k;
  return min(min(a1, a2), min(a3, a4));
}
int bfs() {
  q.push(make_pair(sx, sy));
  num.push(0);
  int ans = 1e9;
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second;
    int numm = num.front();
    q.pop();
    num.pop();
    if (numm > k) continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= n || ny >= m || nx < 0 || ny < 0) continue;
      if (M[nx][ny] == '.') {
        q.push(make_pair(nx, ny));
        num.push(numm + 1);
      }
    }
    if (numm == 0) numm = 1;
    int cnt = (numm + k - 1) / k + way(x, y);
    ans = min(ans, cnt);
  }
  return ans;
}
int main() {
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      cin >> M[i][j];
      if (M[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  cout << bfs() << endl;
  return 0;
}
