#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char second[808][808];
int d[808][808][401];
queue<pair<int, pair<int, int> > > q;
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] = 0;
        q.push(make_pair(i, make_pair(j, 0)));
      }
  while (!q.empty()) {
    int x = q.front().first;
    int y = q.front().second.first;
    int c = q.front().second.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 >= 401 || d[sx][sy][c + 1] != -1) continue;
          d[sx][sy][c + 1] = d[x][y][c] + 1;
          q.push(make_pair(sx, make_pair(sy, c + 1)));
        } else {
          if (c + 1 >= 401 || d[sx][sy][c] != -1) continue;
          d[sx][sy][c] = d[x][y][c] + 1;
          q.push(make_pair(sx, make_pair(sy, c)));
        }
      }
  }
  int ans = int(1e9);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      if (i && j && i + 1 < n && j + 1 < m) continue;
      for (int c = 0; c < 401; ++c) {
        if (d[i][j][c] == -1) continue;
        ans = min(ans, (d[i][j][c] + k - 1) / k + (c + k - 1) / k);
      }
    }
  printf("%d\n", ans);
  return 0;
}
