#include <bits/stdc++.h>
using namespace std;
int movesi[4] = {-1, 0, 0, 1};
int movesj[4] = {0, -1, 1, 0};
int h, w, k;
pair<int, int> memo[800 + 5][800 + 5];
int maze[800 + 5][800 + 5];
void BFS(int i, int j) {
  queue<pair<int, pair<int, int> > > q;
  memo[i][j].first = 1;
  memo[i][j].second = 0;
  q.push(make_pair(i, make_pair(j, 0)));
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second.first;
    int closed = q.front().second.second;
    q.pop();
    for (i = 0; i < 4; i++) {
      int nx = x + movesi[i];
      int ny = y + movesj[i];
      int c = closed;
      if ((nx >= 0 && nx < h) && (ny >= 0 && ny < w)) {
        if (memo[nx][ny].first == 0) {
          memo[nx][ny].first = memo[x][y].first + 1;
          if (maze[nx][ny] == 1) c++;
          memo[nx][ny].second = c;
          q.push(make_pair(nx, make_pair(ny, c)));
        }
      }
    }
  }
}
int main() {
  string s = "";
  int i = 0, j = 0, ini = 0, fin = 0;
  cin >> h >> w >> k;
  for (i = 0; i < h; i++) {
    cin >> s;
    for (j = 0; j < w; j++) {
      if (s[j] == '#')
        maze[i][j] = 1;
      else if (s[j] == 'S') {
        ini = i;
        fin = j;
      }
    }
  }
  BFS(ini, fin);
  pair<int, int> res;
  res.first = res.second = INT_MAX;
  for (i = 0; i < h; i++) res = min(res, memo[0][i]);
  for (i = 0; i < h; i++) res = min(res, memo[w - 1][i]);
  for (i = 0; i < w; i++) res = min(res, memo[i][0]);
  for (i = 0; i < w; i++) res = min(res, memo[i][h - 1]);
  int r = 0;
  if (res.second == 0) {
    r = res.first / k;
    if (res.first % k != 0) r++;
  } else {
    res.first -= 1;
    if (res.first == res.second) {
      r = 1;
      r += res.second / k;
      if (res.second % k != 0) r++;
    } else {
      r += res.second / k;
      if (res.second % k != 0) r++;
      res.first -= (r * k);
      res.first = ((res.first % k == 0) ? 0 : 1) + (res.first / k);
      r += res.first;
    }
  }
  cout << r << endl;
  return 0;
}
