#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 20;
vector<int> adj[maxn];
long long d[maxn][maxn], h, t, res = 1e18, n, m;
string s[maxn];
pair<long long, long long> q[maxn];
bool is(int x, int y, bool k = 0) {
  if (x < 0 || x > n - 1 || y < 0 || y > m - 1 || (k && s[x][y] == '#'))
    return 0;
  return 1;
}
void bfs(vector<pair<int, int> > src) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      d[i][j] = -1;
    }
  h = t = 0;
  for (auto x : src) d[x.first][x.second] = 0, q[h++] = {x.first, x.second};
  while (t < h) {
    int x = q[t].first, y = q[t++].second;
    if (is(x + 1, y)) {
      if (d[x + 1][y] == -1) {
        d[x + 1][y] = d[x][y] + 1;
        q[h++] = {x + 1, y};
      }
    }
    if (is(x - 1, y)) {
      if (d[x - 1][y] == -1) {
        d[x - 1][y] = d[x][y] + 1;
        q[h++] = {x - 1, y};
      }
    }
    if (is(x, y + 1)) {
      if (d[x][y + 1] == -1) {
        d[x][y + 1] = d[x][y] + 1;
        q[h++] = {x, y + 1};
      }
    }
    if (is(x, y - 1)) {
      if (d[x][y - 1] == -1) {
        d[x][y - 1] = d[x][y] + 1;
        q[h++] = {x, y - 1};
      }
    }
  }
}
void bffs(int x, int y) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) d[i][j] = -1;
  d[x][y] = 0;
  q[h++] = {x, y};
  while (t < h) {
    int x = q[t].first, y = q[t++].second;
    if (is(x + 1, y, 1)) {
      if (d[x + 1][y] == -1) {
        d[x + 1][y] = d[x][y] + 1;
        q[h++] = {x + 1, y};
      }
    }
    if (is(x - 1, y, 1)) {
      if (d[x - 1][y] == -1) {
        d[x - 1][y] = d[x][y] + 1;
        q[h++] = {x - 1, y};
      }
    }
    if (is(x, y + 1, 1)) {
      if (d[x][y + 1] == -1) {
        d[x][y + 1] = d[x][y] + 1;
        q[h++] = {x, y + 1};
      }
    }
    if (is(x, y - 1, 1)) {
      if (d[x][y - 1] == -1) {
        d[x][y - 1] = d[x][y] + 1;
        q[h++] = {x, y - 1};
      }
    }
  }
}
int main() {
  int k, x1, y1;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'S') x1 = i, y1 = j;
    }
  }
  bffs(x1, y1);
  vector<pair<int, int> > a;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (d[i][j] != -1 && d[i][j] <= k) a.push_back({i, j});
    }
  bfs(a);
  for (int i = 0; i < n; i++) res = min(res, min(d[i][0], d[i][m - 1]));
  for (int i = 0; i < m; i++) res = min(res, min(d[0][i], d[m - 1][i]));
  res = max(res, 0LL);
  cout << (res / k + (res % k != 0)) + 1;
}
