#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
  int h, w, k, sy, sx;
  bool a[800][800];
  cin >> h >> w >> k;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      a[i][j] = s[j] == '#';
      if (s[j] == 'S') {
        sy = i;
        sx = j;
      }
    }
  }
  pair<int, pair<int, int>> p[h][w];
  fill(p[0], p[h], make_pair(100000, make_pair(100000, 100000)));
  p[sy][sx] = make_pair(0, make_pair(0, 0));
  deque<pair<int, int>> d;
  d.push_back(make_pair(sy, sx));
  while (!d.empty()) {
    int y = d.front().first, x = d.front().second;
    d.pop_front();
    for (int i = 0; i < 4; i++) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
        pair<int, pair<int, int>> q;
        q.second.second = p[y][x].second.second + (int)a[ny][nx];
        if (a[ny][nx] && q.second.second > k * p[y][x].first) {
          q.second.first = k * (p[y][x].first + 1) + 1;
        } else {
          q.second.first = p[y][x].second.first + 1;
        }
        q.first = (q.second.first + k - 1) / k;
        if (q < p[ny][nx]) {
          p[ny][nx] = q;
          d.push_back(make_pair(ny, nx));
        }
      }
    }
  }
  int m = 100000;
  for (int i = 0; i < h; i++) {
    m = min(m, p[i][0].first);
    m = min(m, p[i][w - 1].first);
  }
  for (int i = 0; i < w; i++) {
    m = min(m, p[0][i].first);
    m = min(m, p[h - 1][i].first);
  }
  cout << m << endl;
  return 0;
}
