#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long i, j, k, h, w, a, b, dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1},
                                 ans = 1e9;
  scanf("%lld%lld%lld", &h, &w, &k);
  char s[h][w + 10];
  bool flg[h][w];
  long long d[h][w][2];
  for (i = 0; i < h; ++i)
    for (j = 0; j < w; ++j) d[i][j][0] = d[i][j][1] = 1e9, flg[i][j] = true;
  queue<pair<long long, long long> > q;
  pair<long long, long long> p;
  for (i = 0; i < h; ++i) {
    scanf("%s", s[i]);
    for (j = 0; j < w; ++j)
      if (s[i][j] == 'S')
        q.push(make_pair(i, j)), d[i][j][0] = d[i][j][1] = 0, flg[i][j] = false;
  }
  while (q.size()) {
    p = q.front(), q.pop();
    a = p.first, b = p.second;
    if (!(a * b) || a == h - 1 || b == w - 1) {
      if (ans > d[a][b][0]) ans = d[a][b][0];
      continue;
    }
    if (d[a][b][0]) {
      for (i = 0; i < 4; ++i)
        if (flg[a + dx[i]][b + dy[i]]) {
          if (d[a][b][1] < k)
            d[a + dx[i]][b + dy[i]][0] = d[a][b][0],
                             d[a + dx[i]][b + dy[i]][1] = d[a][b][1] + 1;
          else
            d[a + dx[i]][b + dy[i]][0] = d[a][b][0] + 1,
                             d[a + dx[i]][b + dy[i]][1] = 1;
          flg[a + dx[i]][b + dy[i]] = false;
          q.push(make_pair(a + dx[i], b + dy[i]));
        }
    } else {
      for (i = 0; i < 4; ++i)
        if (flg[a + dx[i]][b + dy[i]]) {
          if (d[a][b][1] < k && s[a + dx[i]][b + dy[i]] == '.')
            d[a + dx[i]][b + dy[i]][0] = d[a][b][0],
                             d[a + dx[i]][b + dy[i]][1] = d[a][b][1] + 1;
          else
            d[a + dx[i]][b + dy[i]][0] = d[a][b][0] + 1,
                             d[a + dx[i]][b + dy[i]][1] = 1;
          flg[a + dx[i]][b + dy[i]] = false;
          q.push(make_pair(a + dx[i], b + dy[i]));
        }
    }
  }
  printf("%lld", ans + 1);
  return 0;
}
