#include <bits/stdc++.h>
using namespace std;
char asd[1005][1005];
int vis[1000][1000];
queue<pair<int, int> > que;
int ar[5] = {1, -1, 0, 0};
int arr[5] = {0, 0, 1, -1};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int z, x, c;
  cin >> z >> x >> c;
  int st1, st2;
  for (int q = 1; q <= z; q++) {
    for (int w = 1; w <= x; w++) {
      cin >> asd[q][w];
      if (asd[q][w] == 'S') st1 = q, st2 = w;
    }
  }
  for (int q = 1; q <= z; q++) {
    for (int w = 1; w <= x; w++) vis[q][w] = INT_MAX;
  }
  que.push(make_pair(st1, st2));
  vis[st1][st2] = 0;
  while (!que.empty()) {
    pair<int, int> temp;
    temp = que.front();
    for (int q = 0; q < 4; q++) {
      if (temp.first + ar[q] >= 1 && temp.first + ar[q] <= z &&
          temp.second + arr[q] >= 1 && temp.second + arr[q] <= x &&
          asd[temp.first + ar[q]][temp.second + arr[q]] == '.') {
        if (vis[temp.first][temp.second] + 1 <= c) {
          vis[temp.first + ar[q]][temp.second + arr[q]] =
              vis[temp.first][temp.second] + 1;
          que.push(make_pair(temp.first + ar[q], temp.second + arr[q]));
        }
      }
    }
    que.pop();
  }
  int ans = INT_MAX;
  for (int q = 1; q <= z; q++) {
    for (int w = 1; w <= x; w++) {
      if (vis[q][w] <= c) {
        int mins = min(q - 1, min(w - 1, min(z - q, x - w)));
        int temp = mins / c;
        if (mins % c != 0) temp++;
        ans = min(ans, temp + 1);
      }
    }
  }
  cout << ans << endl;
}
