#include <bits/stdc++.h>
using namespace std;
void f(vector<string>& v, int y, int x, int nl, int l) {
  if (v[y][x] == '.') {
    v[y][x] = 'S';
    if (nl == l) return;
    if (y != 0 && v[y - 1][x] == '.') {
      f(v, y - 1, x, nl, l + 1);
    }
    if (y != v.size() - 1 && v[y + 1][x] == '.') {
      f(v, y + 1, x, nl, l + 1);
    }
    if (x != 0 && v[y][x - 1] == '.') {
      f(v, y, x - 1, nl, l + 1);
    }
    if (y != v[0].size() - 1 && v[y][x + 1] == '.') {
      f(v, y, x + 1, nl, l + 1);
    }
  }
}
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<string> v;
  int sx, sy;
  for (int i = 0; i < h; ++i) {
    string str;
    cin >> str;
    for (int j = 0; j < str.size(); ++j) {
      if (str[j] == 'S') {
        sx = j;
        sy = i;
      }
    }
    v.push_back(str);
  }
  v[sy][sx] = '.';
  f(v, sy, sx, k, 0);
  int minl = h;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (v[i][j] == 'S') {
        if (minl > i) {
          minl = i;
        }
        if (minl > h - 1 - i) {
          minl = h - 1 - i;
        }
        if (minl > j) {
          minl = j;
        }
        if (minl > w - 1 - j) {
          minl = w - 1 - j;
        }
      }
    }
  }
  if (minl == 0) {
    cout << 1 << endl;
  } else {
    cout << (minl - 1) / k + 2 << endl;
  }
  return 0;
}
