#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int N = 810;
struct info {
  int x, y, step, run, loc;
  bool operator<(const info &rhs) const { return step > rhs.step; }
  info operator+(info t) {
    t.x += x;
    t.y += y;
    t.step += step;
    t.run += run;
    t.loc += loc;
    return t;
  }
};
info S,
    dir[4] = {
        {1, 0, 0, 0, 0}, {-1, 0, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, -1, 0, 0, 0}};
char pos[N][N];
int vis[N][N];
int n, m, k;
bool check(info t) {
  return t.x >= 0 && t.x < n && t.y >= 0 && t.y < m && !vis[t.x][t.y];
}
void init() { memset(vis, 0, sizeof(vis)); }
int bfs() {
  priority_queue<info> Q;
  Q.push(S);
  vis[S.x][S.y] = 1;
  while (!Q.empty()) {
    info u = Q.top();
    Q.pop();
    vis[u.x][u.y] = 1;
    if (u.x == 0 || u.x == n - 1 || u.y == 0 || u.y == m - 1) return u.step;
    for (int i = 0; i < 4; ++i) {
      info v = u + dir[i];
      if (!check(v)) continue;
      if (pos[v.x][v.y] == '.') {
        ++v.run;
        if (v.run == 1)
          ++v.step;
        else if (v.run > k) {
          v.run = 1;
          v.loc = 0;
          ++v.step;
        }
        Q.push(v);
      } else {
        ++v.loc;
        pos[v.x][v.y] = '.';
        if (v.loc == 1) {
          v.run = 1;
          v.loc = 1;
          v.step += 2;
        } else if (v.loc > k) {
          v.loc = 1;
          v.run = 1;
          v.step++;
        }
        Q.push(v);
      }
    }
  }
  return 0;
}
int main(void) {
  int i, j;
  while (~scanf("%d%d%d", &n, &m, &k)) {
    init();
    for (i = 0; i < n; ++i) {
      scanf("%s", pos[i]);
      for (j = 0; j < m; ++j) {
        if (pos[i][j] == 'S') {
          S.x = i;
          S.y = j;
          S.step = 0;
          S.loc = 0;
          S.run = 0;
        }
      }
    }
    printf("%d\n", bfs());
  }
  return 0;
}
