#include <bits/stdc++.h>
using namespace std;
int h, w, k;
bool f[801][801];
vector<string> gr(801);
vector<pair<int, int>> rqu;
queue<pair<int, int>> qu;
int ri, rj;
void proc(int ro, int co, int ir, int jr) {
  if (!f[ro + ir][co + jr] && gr[ro + ir][co + jr] == '.') {
    qu.push(make_pair(ro + ir, co + jr));
    rqu.push_back(make_pair(ro + ir, co + jr));
    f[ro + ir][co + jr] = 1;
  }
}
int calc(int x, int y) {
  if (x == 1 || x == h || y == 1 || y == w) return 1;
  x = min(x - 1, h - x);
  y = min(y - 1, w - y);
  x = min(x, y);
  if (x % k != 0) {
    x = x / k + 1;
  } else {
    x = x / k;
  }
  x++;
  return x;
}
int main() {
  cin >> h >> w >> k;
  for (int i = 1; i <= h; i++) {
    cin >> gr[i];
    gr[i] = " " + gr[i];
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (gr[i][j] == 'S') {
        ri = i;
        rj = j;
      }
    }
  }
  qu.push(make_pair(ri, rj));
  f[ri][rj] = 1;
  int s;
  for (int i = 0; i < k; i++) {
    s = qu.size();
    for (int i = 0; i < s; i++) {
      proc(qu.front().first, qu.front().second, 1, 0);
      proc(qu.front().first, qu.front().second, -1, 0);
      proc(qu.front().first, qu.front().second, 0, 1);
      proc(qu.front().first, qu.front().second, 0, -1);
      qu.pop();
    }
  }
  int res = 1000000000;
  int x, y;
  if (rqu.empty()) {
    x = min(ri - 1, h - ri);
    y = min(rj - 1, w - rj);
    x = min(x, y);
    if (x % k != 0) {
      x = x / k + 1;
    } else {
      x = x / k;
    }
    x++;
    cout << x;
    return 0;
  } else {
    for (int i = 0; i < rqu.size(); i++) {
      res = min(res, calc(rqu[i].first, rqu[i].second));
    }
  }
  cout << res;
  return 0;
}
