#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int h, w, k;
int stx = -1, sty, ans = 0x3f3f3f3f;
int mov[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
struct node {
  int x, y, step;
} cur;
void bfs() {
  memset(vis, false, sizeof(vis));
  queue<node> q;
  q.push(node{stx, sty, 0});
  while (!q.empty()) {
    cur = q.front();
    q.pop();
    int x = cur.x, y = cur.y;
    if (x == 1 || x == h || y == 1 || y == w) ans = min(ans, cur.step);
    for (int i = 0; i < 4; ++i) {
      int nx = x + mov[i][0], ny = y + mov[i][1];
      if (nx < 1 || nx > h || ny < 1 || ny > w) continue;
      if (vis[nx][ny]) continue;
      if (mp[nx][ny] == '.')
        q.push(node{nx, ny, cur.step + 1}), vis[nx][ny] = 1;
      else {
        for (int j = 1; j <= k; ++j)
          mp[x + j * mov[i][0]][y + j * mov[i][1]] = '.';
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &h, &w, &k);
  for (int i = 1; i <= h; ++i) {
    scanf("%s", mp[i] + 1);
    if (stx != -1) continue;
    for (int j = 1; mp[i][j]; ++j)
      if (mp[i][j] == 'S') {
        stx = i, sty = j;
        break;
      }
  }
  bfs();
  printf("%d\n", ans);
  return 0;
}
