#include <bits/stdc++.h>
using namespace std;
int in() {
  int a;
  scanf("%d ", &a);
  return a;
}
vector<int> v;
int H, W;
int K;
int magic(int i, int k) {
  int ydist = min(i - 1, H - i);
  int xdist = min(k - 1, W - k);
  int dist = min(ydist, xdist);
  return (dist + K - 1) / K;
}
int main() {
  H = in();
  W = in();
  K = in();
  vector<vector<int> > v(810, vector<int>(810, 0));
  for (int i = 0; i < H + 2; i++) v[i][0] = v[i][W + 1] = -1;
  for (int i = 0; i < W + 2; i++) v[0][i] = v[H + 1][i] = -1;
  int y = -1, x = -1;
  for (int i = 1; i <= H; i++) {
    char temp[810];
    gets(temp);
    for (int k = 1; k <= W; k++) {
      char c = temp[k - 1];
      if (c == 'S') {
        y = i;
        x = k;
      } else if (c == '#') {
        v[i][k] = 99;
      }
    }
  }
  assert(y >= 0 && x >= 0);
  int ans = magic(y, x);
  stack<pair<int, int> > st;
  st.push(make_pair(y, x));
  for (int i = 0; i < K + 1; i++) {
    stack<pair<int, int> > st2;
    while (!st.empty()) {
      pair<int, int> p = st.top();
      st.pop();
      v[p.first][p.second] = 1;
      ans = min(ans, magic(p.first, p.second));
      for (int dy = -1; dy <= 1; dy++) {
        for (int dx = -1; dx <= 1; dx++) {
          if (dy * dx == 0 && v[p.first + dy][p.second + dx] == 0) {
            st2.push(make_pair(p.first + dy, p.second + dx));
          }
        }
      }
    }
    st = st2;
  }
  cout << ans + 1 << endl;
  return 0;
}
