#include <bits/stdc++.h>
using namespace std;
class Data {
 public:
  int cost;
  int power;
  int hakai;
  int x;
  int y;
  bool operator>(const Data &data) const { return this->cost > data.cost; }
};
Data make_data(int c, int p, int h, int x, int y) {
  Data d;
  d.cost = c;
  d.power = p;
  d.hakai = h;
  d.x = x;
  d.y = y;
  return d;
}
char mas[810][810];
int memo[810][810];
int move_x[4] = {1, -1, 0, 0};
int move_y[4] = {0, 0, 1, -1};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy;
  for (int i = 0; i < h; i++) {
    cin >> mas[i];
    for (int j = 0; j < w; j++) {
      if (mas[i][j] == 'S') {
        mas[i][j] = '.';
        sy = i;
        sx = j;
      }
    }
  }
  priority_queue<Data, vector<Data>, greater<Data> > que;
  memset(memo, -1, sizeof(memo));
  que.push(make_data(1, k, 0, sx, sy));
  while (!que.empty()) {
    int cost = que.top().cost;
    int power = que.top().power;
    int hakai = que.top().hakai;
    int x = que.top().x;
    int y = que.top().y;
    que.pop();
    if (x == 0 || x == w - 1 || y == 0 || y == h - 1) {
      cout << cost << endl;
      return 0;
    }
    if (memo[y][x] != -1 && memo[y][x] < cost) continue;
    memo[y][x] = cost;
    for (int i = 0; i < 4; i++) {
      int nx = x + move_x[i];
      int ny = y + move_y[i];
      int nhakai = hakai;
      int ncost = cost;
      int npower = power;
      if (mas[ny][nx] == '#') {
        nhakai--;
        if (hakai == -1) {
          ncost++;
          nhakai = k;
          npower = k;
        }
      }
      npower--;
      if (power == -1) {
        ncost++;
        nhakai += k;
        npower = k;
      }
      que.push(make_data(ncost, npower, nhakai, nx, ny));
    }
  }
  return 0;
}
