#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  string a[h];
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }
  int s;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == 'S') {
        s = i * w + j;
        break;
      }
    }
  }
  pair<int, int> c[h][w];
  int inf = 1e5;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      c[i][j] = make_pair(inf, inf);
    }
  }
  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      pq;
  pq.push(make_tuple(0, 0, s));
  while (!pq.empty()) {
    int c1, c2, p;
    tie(c1, c2, p) = pq.top();
    pq.pop();
    int y = p / w;
    int x = p % w;
    if (make_pair(c1, c2) >= c[y][x]) {
      continue;
    }
    c[y][x] = make_pair(c1, c2);
    if (y > 0) {
      int new_c1 = c1;
      int new_c2 = c2;
      ++new_c1;
      if (a[y - 1][x] == '#') {
        ++new_c2;
      }
      if (make_pair(new_c1, new_c2) < c[y - 1][x]) {
        pq.push(make_tuple(new_c1, new_c2, (y - 1) * w + x));
      }
    }
    if (y < h - 1) {
      int new_c1 = c1;
      int new_c2 = c2;
      ++new_c1;
      if (a[y + 1][x] == '#') {
        ++new_c2;
      }
      if (make_pair(new_c1, new_c2) < c[y + 1][x]) {
        pq.push(make_tuple(new_c1, new_c2, (y + 1) * w + x));
      }
    }
    if (x > 0) {
      int new_c1 = c1;
      int new_c2 = c2;
      ++new_c1;
      if (a[y][x - 1] == '#') {
        ++new_c2;
      }
      if (make_pair(new_c1, new_c2) < c[y][x - 1]) {
        pq.push(make_tuple(new_c1, new_c2, y * w + x - 1));
      }
    }
    if (x < w - 1) {
      int new_c1 = c1;
      int new_c2 = c2;
      ++new_c1;
      if (a[y][x + 1] == '#') {
        ++new_c2;
      }
      if (make_pair(new_c1, new_c2) < c[y][x + 1]) {
        pq.push(make_tuple(new_c1, new_c2, y * w + x + 1));
      }
    }
  }
  int ans = 1e5;
  for (int i = 0; i < h; i++) {
    int a, b;
    tie(a, b) = c[i][0];
    int tmp = (a - 1) / k + 1;
    if (b > 0) {
      tmp = max((b - 1) / k + 2, tmp);
    }
    ans = min(tmp, ans);
  }
  for (int i = 0; i < h; i++) {
    int a, b;
    tie(a, b) = c[i][w - 1];
    int tmp = (a - 1) / k + 1;
    if (b > 0) {
      tmp = max((b - 1) / k + 2, tmp);
    }
    ans = min(tmp, ans);
  }
  for (int i = 0; i < w; i++) {
    int a, b;
    tie(a, b) = c[0][i];
    int tmp = (a - 1) / k + 1;
    if (b > 0) {
      tmp = max((b - 1) / k + 2, tmp);
    }
    ans = min(tmp, ans);
  }
  for (int i = 0; i < w; i++) {
    int a, b;
    tie(a, b) = c[h - 1][i];
    int tmp = (a - 1) / k + 1;
    if (b > 0) {
      tmp = max((b - 1) / k + 2, tmp);
    }
    ans = min(tmp, ans);
  }
  cout << ans << endl;
}
