#include <bits/stdc++.h>
using namespace std;
long long int INF = 3e18;
long long int mod = 4294967296;
double Pi = 3.1415926535897932384626;
vector<long long> G[500005];
vector<pair<long long, long long> > tree[500010];
priority_queue<long long> pql;
priority_queue<pair<long long, long long> > pqp;
priority_queue<long long, vector<long long>, greater<long long> > pqls;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pqps;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
long long rui(long long abc, long long bed) {
  if (bed == 0) {
    return 1;
  } else {
    long long ced = rui(abc, bed / 2);
    ced *= ced;
    ced %= mod;
    if (bed % 2 == 1) {
      ced *= abc;
      ced %= mod;
    }
    return ced;
  }
}
long long gcd(long long number1, long long number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}
void YES(bool condition) {
  if (condition) {
    cout << "YES" << endl;
    ;
  } else {
    cout << "NO" << endl;
    ;
  }
  return;
}
void Yes(bool condition) {
  if (condition) {
    cout << "Yes" << endl;
    ;
  } else {
    cout << "No" << endl;
    ;
  }
  return;
}
long long n, m, num, sum, ans, a, b, c, d, e, f, g, h, i, j, k, w;
long long x[500005], y[500005];
long long dp[805][805];
bool flag, dame;
bool ban[505][505];
char s[805][805];
int main() {
  cin >> h >> w >> k;
  for (i = 1; i <= h; i++) {
    cin >> s[i];
    for (j = w; j >= 1; j--) {
      s[i][j] = s[i][j - 1];
    }
  }
  ans = INF;
  long long sx = -1, sy = -1;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      dp[i][j] = -1;
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
        break;
      }
    }
  }
  pqps.push(pair<long long, long long>(0ll, sx * 1000 + sy));
  while (pqps.size()) {
    pair<long long, long long> p = pqps.top();
    pqps.pop();
    long long x, y, cost;
    cost = p.first;
    num = p.second;
    x = num / 1000;
    y = num % 1000;
    if (dp[x][y] != -1) continue;
    dp[x][y] = cost;
    long long ansx, ansy;
    ansx = min(x - 1, h - x);
    ansy = min(y - 1, w - y);
    ans = min(ans, min(ansx, ansy));
    if (cost == k) continue;
    if (x == 1 || x == h || y == 1 || y == w) {
      cout << 1 << endl;
      ;
      return 0;
    } else {
      long long nx, ny;
      for (int i = 0; i < 4; i++) {
        nx = x + dx[i];
        ny = y + dy[i];
        if (s[nx][ny] != '#' && 1 <= nx && nx <= h && 1 <= ny && ny <= w) {
          pqps.push(pair<long long, long long>(cost + 1, nx * 1000 + ny));
        }
      }
    }
  }
  cout << (ans + k - 1) / k + 1 << endl;
  ;
  return 0;
}
