#include <bits/stdc++.h>
using namespace std;
char s[810][810];
int n, m, K, sx, sy, ans = 1000000000;
int dis[810][810];
map<int, int> G[810][810];
map<int, int>::iterator it, itt;
struct node {
  int x, y, r1, r2;
  node() {}
  node(int _x = 0, int _y = 0, int _r1 = 0, int _r2 = 0) {
    x = _x, y = _y, r1 = _r1, r2 = _r2;
  }
  bool operator<(const node &a) const { return r1 > a.r1; }
};
priority_queue<node> Q;
int d[] = {-1, 0, 1, 0, -1};
void Min(int &x, int y) {
  if (x == 0 || x > y) x = y;
}
void BFS() {
  dis[sx][sy] = 1;
  G[sx][sy][0] = 0;
  Q.push(node(sx, sy, 0, 0));
  while (!Q.empty()) {
    node p = Q.top();
    Q.pop();
    if (G[p.x][p.y].find(p.r1) == G[p.x][p.y].end() || G[p.x][p.y][p.r1] < p.r2)
      continue;
    if (max((p.r1 + K - 1) / K, (p.r2 + K - 1) / K + 1) >= ans) continue;
    for (int i = 0; i < 4; i++) {
      int x = p.x + d[i], y = p.y + d[i + 1];
      int r1 = p.r1 + 1, r2 = p.r2 + (s[x][y] == '#');
      if (x == 1 || x == n || y == 1 || y == m) {
        ans = min(ans, max((r1 + K - 1) / K, (r2 + K - 1) / K + 1));
        continue;
      }
      if (dis[x][y] != 0 && (r1 + K - 1) / K > dis[x][y]) continue;
      it = G[x][y].lower_bound(r1);
      if (it->first == r1 && it->second <= r2) continue;
      if (it != G[x][y].begin()) {
        itt = it;
        itt--;
        if (itt->second <= r2) continue;
      }
      while (it != G[x][y].end() && it->second >= r2) G[x][y].erase(it++);
      G[x][y][r1] = r2;
      Min(dis[x][y], max((r1 + K - 1) / K, (r2 + K - 1) / K + 1));
      Q.push(node(x, y, r1, r2));
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'S') sx = i, sy = j;
  }
  if (sx == 1 || sx == n || sy == 1 || sy == m) {
    printf("0\n");
    return 0;
  }
  BFS();
  printf("%d\n", ans);
}
