#include <bits/stdc++.h>
using namespace std;
string A[800];
int d[800][800];
int h_moves[4] = {-1, 1, 0, 0};
int w_moves[4] = {0, 0, -1, 1};
int dijikstra(int s_h, int s_w, int H, int W) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      d[i][j] = INT_MAX;
    }
  }
  priority_queue<pair<int, pair<int, int> >,
                 vector<pair<int, pair<int, int> > >,
                 greater<pair<int, pair<int, int> > > >
      pq;
  d[s_h][s_w] = 0;
  pq.push(make_pair(0, make_pair(s_h, s_w)));
  while (!pq.empty()) {
    auto cur = pq.top();
    auto cur_cost = cur.first;
    auto cur_h = cur.second.first;
    auto cur_w = cur.second.second;
    pq.pop();
    if (cur_h == 0 || cur_h == H - 1 || cur_w == 0 || cur_w == W) {
      return cur_cost;
    }
    if (d[cur_h][cur_w] < cur_cost) {
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int next_h = cur_h + h_moves[i];
      int next_w = cur_w + w_moves[i];
      if (0 <= next_h && next_h < H && 0 <= next_w && next_w < W) {
        int next_cost = cur_cost;
        if (A[next_h][next_w] == '#') {
          next_cost++;
        }
        if (d[next_h][next_w] > next_cost) {
          d[next_h][next_w] = next_cost;
          pq.push(make_pair(next_cost, make_pair(next_h, next_w)));
        }
      }
    }
  }
  return INT_MAX;
}
int main() {
  int H;
  int W;
  int K;
  cin >> H >> W >> K;
  for (int i = 0; i < H; i++) {
    cin >> A[i];
  }
  int s_h;
  int s_w;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (A[i][j] == 'S') {
        s_h = i;
        s_w = j;
        break;
      }
    }
  }
  int steps = dijikstra(s_h, s_w, H, W);
  if (steps % K == 0) {
    cout << steps / K << endl;
  } else {
    cout << steps / K + 1 << endl;
  }
  return 0;
}
