#include <bits/stdc++.h>
using namespace std;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x7f7f7f7f;
int n, m, k;
char mapp[805][805];
int vis[805][805];
int dic[][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
set<int> vec;
struct node {
  int x, y, step;
};
void bfs(int x, int y) {
  vis[x][y] = 1;
  queue<node> q;
  node p1;
  p1.x = x, p1.y = y, p1.step = 0;
  q.push(p1);
  while (!q.empty()) {
    node p = q.front();
    q.pop();
    for (int i = 0; i < 4; i++) {
      int kk = 0;
      for (int j = 1; j <= k; j++) {
        int xx = p.x + dic[i][0] * j;
        int yy = p.y + dic[i][1] * j;
        if (xx >= 0 && xx < n && yy >= 0 && yy < m && !vis[xx][yy]) {
          if ((xx == 0 || xx == n - 1 || yy == 1 || yy == m - 1) &&
              mapp[xx][yy] == '.') {
            vec.insert(p.step + 1);
            continue;
          }
          if (mapp[xx][yy] != '#') {
            vis[xx][yy] = 1;
            node p1;
            p1.x = xx;
            p1.y = yy;
            p1.step = p.step + 1;
            q.push(p1);
          } else if (mapp[xx][yy] == '#') {
            xx -= dic[i][0];
            yy -= dic[i][1];
            vis[xx][yy] = 1;
            node p1;
            p1.x = xx;
            p1.y = yy;
            p1.step = p.step + 1;
            q.push(p1);
            while (kk < k) {
              xx = xx + dic[i][0];
              yy = yy + dic[i][1];
              if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
                if (mapp[xx][yy] == '#') {
                  mapp[xx][yy] = '.';
                  kk++;
                }
              } else {
                break;
              }
            }
            break;
          } else if (k == j) {
            vis[xx][yy] = 1;
            node p1;
            p1.x = xx;
            p1.y = yy;
            p1.step = p.step + 1;
            q.push(p1);
            while (kk < k) {
              xx = xx + dic[i][0];
              yy = yy + dic[i][1];
              if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
                if (mapp[xx][yy] == '#') {
                  mapp[xx][yy] = '.';
                  kk++;
                }
              } else {
                break;
              }
            }
            break;
          }
        }
      }
    }
  }
  printf("%d\n", *vec.begin());
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", mapp[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      if (mapp[i][j] == 'S') {
        bfs(i, j);
        return 0;
      }
    }
  return 0;
}
