#include <bits/stdc++.h>
using namespace std;
const int inf = 2147483647, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
const int N = 100005, M = 1000005, mod = 1000000007;
const long long llinf = 9223372036854775807ll;
int w, h, k, sx, sy, px, py, mind, mind2, ans, unl;
char a[805][805];
bool u[805][805];
void dfs(int x, int y, int t) {
  int dist = min(min(x - 1, y - 1), min(w - x, h - y));
  if (dist < mind) mind = dist, px = x, py = y, mind2 = t;
  u[x][y] = true;
  for (int d = 0; d < 4; d++) {
    int xx = x + dx[d], yy = y + dy[d];
    if (xx < 1 || xx > w || yy < 1 || yy > h || u[xx][yy] || a[xx][yy] == '#')
      continue;
    dfs(xx, yy, t + 1);
  }
}
int main() {
  mind = 99999999;
  cin >> w >> h >> k;
  for (int i = 1; i <= w; i++)
    for (int j = 1; j <= h; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'S') sx = i, sy = j, a[i][j] = '.';
    }
  dfs(sx, sy, 0);
  if (mind == 0) {
    cout << mind2 / k + (mind2 % k == 0 ? 0 : 1);
    return 0;
  }
  if (mind2 == 0) {
    cout << mind / k + (mind2 / k) + (mind2 % k == 0 ? 0 : 1) +
                (mind % k == 0 ? 0 : 1);
  }
  ans += mind2 / k;
  unl = ans * k;
  unl = mind - unl;
  cout << (mind2 + mind) / k;
  return 0;
}
