#include <bits/stdc++.h>
using namespace std;
const int64_t INF = 10000000000000000;
template <class T>
void read_1d(std::vector<T>& a, int64_t num) {
  a.reserve(num);
  for (int64_t i1 = 0; i1 < num; i1++) {
    T temp;
    cin >> temp;
    a.push_back(temp);
  }
}
template <class T>
void read_2d(std::vector<std::vector<T> >& a, int64_t x, int64_t y) {
  a.resize(x);
  for (int64_t i1 = 0; i1 < x; i1++) {
    a[i1].reserve(y);
    for (int64_t i2 = 0; i2 < y; i2++) {
      T temp;
      cin >> temp;
      a[i1].push_back(temp);
    }
  }
}
template <class InputIterator>
void debug(InputIterator first, InputIterator last) {
  for (auto i1 = first; i1 != last; i1++) {
    cout << (*i1) << endl;
  }
}
struct Point {
  int64_t x;
  int64_t y;
  int64_t c_cost;
};
bool operator<(const Point& a, const Point& b) { return a.c_cost > b.c_cost; }
int main() {
  int64_t H, W, K;
  cin >> H >> W >> K;
  vector<vector<char> > map;
  read_2d(map, H, W);
  bool used[H][W];
  for (int64_t i1 = 0; i1 < H; i1++) {
    for (int64_t i2 = 0; i2 < W; i2++) {
      used[i1][i2] = false;
    }
  }
  int64_t x1, y1;
  for (int64_t i1 = 0; i1 < H; i1++) {
    for (int64_t i2 = 0; i2 < W; i2++) {
      if (map[i1][i2] == 'S') {
        x1 = i1;
        y1 = i2;
        break;
      }
    }
  }
  priority_queue<Point> que;
  int64_t ans = INF;
  que.push(Point{x1, y1, 0});
  while (!que.empty()) {
    Point p = que.top();
    que.pop();
    if (used[p.x][p.y]) {
      continue;
    }
    if (p.x == 0 || p.x == H - 1 || p.y == 0 || p.y == W - 1) {
      ans = 0;
      break;
    } else {
      used[p.x][p.y] = true;
      ans = min(ans, (p.x - 1) / K + 1);
      ans = min(ans, (p.y - 1) / K + 1);
      ans = min(ans, H - 2 - p.x / K + 1);
      ans = min(ans, W - 2 - p.y / K + 1);
      if (p.c_cost < K) {
        if (!used[p.x - 1][p.y] && map[p.x - 1][p.y] == '.') {
          que.push(Point{p.x - 1, p.y, p.c_cost + 1});
        }
        if (!used[p.x][p.y - 1] && map[p.x][p.y - 1] == '.') {
          que.push(Point{p.x, p.y - 1, p.c_cost + 1});
        }
        if (!used[p.x + 1][p.y] && map[p.x + 1][p.y] == '.') {
          que.push(Point{p.x + 1, p.y, p.c_cost + 1});
        }
        if (!used[p.x][p.y + 1] && map[p.x][p.y + 1] == '.') {
          que.push(Point{p.x, p.y + 1, p.c_cost + 1});
        }
      }
    }
  }
  cout << ans + 1 << endl;
}
