#include <bits/stdc++.h>
int MOD = 1000000000 + 7;
int INF = 1e9;
using namespace std;
string alphabet("abcdefghijklmnopqrstuvwxyz");
char a[1000][1000] = {};
int h, w, k;
pair<int, int> search() {
  for (int i = (1); i <= (h); ++i) {
    for (int j = (1); j <= (w); ++j) {
      if (a[i][j] == 'S') return {i, j};
    }
  }
}
void start() {
  pair<int, int> temp = search();
  queue<tuple<int, int, int> > q;
  q.push(make_tuple(temp.first, temp.second, 0));
  for (int j = (0); j <= (k - 1); ++j) {
    for (int i = (0); i <= (q.size() - 1); ++i) {
      tuple<int, int, int> t = q.front();
      q.pop();
      if (get<0>(t) + 1 != h + 1 && a[get<0>(t) + 1][get<1>(t)] == '.') {
        a[get<0>(t) + 1][get<1>(t)] = 'S';
        if (get<2>(t) < k)
          q.push(make_tuple(get<0>(t) + 1, get<1>(t), get<2>(t) + 1));
      }
      if (get<0>(t) - 1 != 0 && a[get<0>(t) - 1][get<1>(t)] == '.') {
        a[get<0>(t) - 1][get<1>(t)] = 'S';
        if (get<2>(t) < k)
          q.push(make_tuple(get<0>(t) - 1, get<1>(t), get<2>(t) + 1));
      }
      if (get<1>(t) + 1 != w + 1 && a[get<0>(t)][get<1>(t) + 1] == '.') {
        a[get<0>(t)][get<1>(t) + 1] = 'S';
        if (get<2>(t) < k)
          q.push(make_tuple(get<0>(t), get<1>(t) + 1, get<2>(t) + 1));
      }
      if (get<1>(t) - 1 != 0 && a[get<0>(t)][get<1>(t) - 1] == '.') {
        a[get<0>(t)][get<1>(t) - 1] = 'S';
        if (get<2>(t) < k)
          q.push(make_tuple(get<0>(t), get<1>(t) - 1, get<2>(t) + 1));
      }
    }
  }
}
int GO() {
  int ans = INF;
  for (int i = (1); i <= (h); ++i) {
    for (int j = (1); j <= (w); ++j) {
      if (a[i][j] == 'S') {
        if (i == 1 || i == h || j == 1 || j == w)
          ans = 1;
        else {
          ans = 1 + (min({ans, i - 1, h - i, j - 1, w - j}) + k - 1) / k;
        }
      }
    }
  }
  return ans;
}
int main() {
  cin >> h >> w >> k;
  for (int i = (1); i <= (h); ++i) {
    for (int j = (1); j <= (w); ++j) {
      cin >> a[i][j];
    }
  }
  start();
  cout << GO();
  return 0;
}
