#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char second[808][808];
int d[808][808];
int dd[808][808];
queue<pair<int, int> > q, qq;
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%s", second[i]);
  }
  memset(d, -1, sizeof(d));
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      if (second[i][j] == 'S') {
        d[i][j] = 0;
        q.push(make_pair(i, j));
      }
  int ans = int(1e9);
  for (int c = 0; !q.empty(); ++c) {
    memset(dd, -1, sizeof(dd));
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      for (int dx = -1; dx <= 1; ++dx)
        for (int dy = -1; dy <= 1; ++dy) {
          if (abs(dx) + abs(dy) != 1) continue;
          int sx = x + dx;
          int sy = y + dy;
          if (sx < 0 || sx >= n || sy < 0 || sy >= m) continue;
          if (second[sx][sy] == '#') {
            if (c + 1 >= 101 || dd[sx][sy] != -1) continue;
            dd[sx][sy] = d[x][y] + 1;
            qq.push(make_pair(sx, sy));
          } else {
            if (d[sx][sy] != -1) continue;
            d[sx][sy] = d[x][y] + 1;
            q.push(make_pair(sx, sy));
          }
        }
    }
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
        if (i && j && i + 1 < n && j + 1 < m) continue;
        if (d[i][j] == -1) continue;
        ans = min(ans, (d[i][j] + k - 1) / k + (c + k - 1) / k);
      }
    qq.swap(q);
    memcpy(d, dd, sizeof(dd));
  }
  printf("%d\n", ans);
  return 0;
}
