#include <bits/stdc++.h>
using namespace std;
int fx[5] = {0, 0, 1, -1};
int fy[5] = {1, -1, 0, 0};
int n, i, j, m, k, stx, sty, s, t, xx, yy, x, y, ans, cost;
bool a[810][810], g[810][810];
int d[810][810], p[700000], q[700000];
char ch;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  getchar();
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      ch = getchar();
      if (ch == '.')
        a[i][j] = 1;
      else if (ch == 'S')
        a[i][j] = 1, stx = i, sty = j;
    }
    getchar();
  }
  if (stx == 1 || sty == 1 || stx == n || sty == m) {
    puts("0");
    return 0;
  }
  p[1] = stx;
  q[1] = sty;
  cost = min(min((stx - 1 + k - 1) / k, (n - stx + k - 1) / k),
             min((sty - 1 + k - 1) / k, (m - sty + k - 1) / k)) +
         1;
  s = 0;
  t = 1;
  g[stx][sty] = 1;
  while (s < t) {
    x = p[++s];
    y = q[s];
    if (d[x][y] > k) break;
    for (j = 1; j <= 4; j++) {
      xx = x + fx[j];
      yy = y + fy[j];
      if (a[xx][yy] && (stx == 1 || sty == 1 || stx == n || sty == m)) {
        puts("1");
        return 0;
      }
      if (a[xx][yy] && !g[xx][yy]) {
        g[xx][yy] = 1;
        cost =
            min(cost, 1 + min(min((xx - 1 + k - 1) / k, (n - xx + k - 1) / k),
                              min((yy - 1 + k - 1) / k, (m - yy + k - 1) / k)));
        p[++t] = xx;
        q[t] = yy;
        d[xx][yy] = d[x][y] + 1;
      }
    }
  }
  printf("%d\n", cost);
  return 0;
}
