#include <bits/stdc++.h>
using namespace std;

typedef tuple<int, int, int> ti;
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<ti, vector<ti>, greater<ti>> 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 || y == h - 1 || x == 0 || x == w - 1) {
      cout<<1<<endl;
      return 0;
    }
    if (c1 == k) {
      continue;
    }
    if (a[y-1][x] != '#') {
      int new_c1 = c1;
      ++new_c1;
      if (make_pair(new_c1, 0) < c[y-1][x]) {
        pq.push(make_tuple(new_c1, 0, (y - 1) * w + x));
      }
    }
    if (a[y+1][x] != '#') {
      int new_c1 = c1;
      ++new_c1;
      if (make_pair(new_c1, 0) < c[y+1][x]) {
        pq.push(make_tuple(new_c1, 0, (y + 1) * w + x));
      }
    }
    if (a[y][x-1] != '#') {
      int new_c1 = c1;
      ++new_c1;
      if (make_pair(new_c1, 0) < c[y][x-1]) {
        pq.push(make_tuple(new_c1, 0, y * w + x - 1));
      }
    }
    if (a[y][x+1] != '#') {
      int new_c1 = c1;
      ++new_c1;
      if (make_pair(new_c1, 0) < c[y][x+1]) {
        pq.push(make_tuple(new_c1, 0, y * w + x + 1));
      }
    }
  }
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      if (c[i][j] < make_pair(inf, inf)) {
        pq.push(make_tuple(0, 0, i * w + j));
        c[i][j] = make_pair(inf, inf);
      }
    }
  }
  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));
      }
    }
  }
  // for (int i=0; i<h; i++) {
  //   for (int j=0; j<w; j++) {
  //     printf("(%d %d)", c[i][j].first,c[i][j].second);
  //   }
  //   cout<<endl;
  // }
  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 + 1, 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 + 1, 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 + 1, 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 + 1, tmp);
    }
    ans = min(tmp, ans);
  }
  cout<<ans + 1<<endl;
}

Measured memory_bytes: 7800