#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;
const int N = 801;
string a[N];
int tbl[N][N];
int main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  for (int i = (int)(0); i < (int)(h); i++) {
    cin >> a[i];
  }
  int sx = -1, sy = -1;
  for (int i = (int)(0); i < (int)(h); i++) {
    for (int j = (int)(0); j < (int)(w); j++) {
      if (a[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  for (int i = (int)(0); i < (int)(h); i++) {
    for (int j = (int)(0); j < (int)(w); j++) {
      tbl[i][j] = inf;
    }
  }
  queue<pair<pair<int, int>, int> > que;
  que.push(pair<pair<int, int>, int>(pair<int, int>(sx, sy), 0));
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  while (not que.empty()) {
    pair<pair<int, int>, int> t = que.front();
    que.pop();
    int x = t.first.first;
    int y = t.first.second;
    int turn = t.second;
    if (tbl[x][y] <= turn) {
      continue;
    }
    tbl[x][y] = turn;
    for (int d = (int)(0); d < (int)(4); d++) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
        continue;
      }
      if (a[nx][ny] == '#') {
        continue;
      }
      que.push(pair<pair<int, int>, int>(pair<int, int>(nx, ny), turn + 1));
    }
  }
  int mi = inf;
  for (int i = (int)(0); i < (int)(h); i++) {
    mi = min(mi, tbl[i][0]);
    mi = min(mi, tbl[i][w - 1]);
  }
  for (int i = (int)(0); i < (int)(w); i++) {
    mi = min(mi, tbl[0][i]);
    mi = min(mi, tbl[h - 1][i]);
  }
  if (mi != inf) {
    cout << (mi + k - 1) / k << endl;
    return 0;
  }
  for (int i = (int)(0); i < (int)(h); i++) {
    for (int j = (int)(0); j < (int)(w); j++) {
      if (tbl[i][j] != inf) {
        que.push(pair<pair<int, int>, int>(pair<int, int>(i, j), k));
      }
      tbl[i][j] = inf;
    }
  }
  while (not que.empty()) {
    pair<pair<int, int>, int> t = que.front();
    que.pop();
    int x = t.first.first;
    int y = t.first.second;
    int turn = t.second;
    if (tbl[x][y] <= turn) {
      continue;
    }
    tbl[x][y] = turn;
    for (int d = (int)(0); d < (int)(4); d++) {
      int nx = x + dx[d];
      int ny = y + dy[d];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
        continue;
      }
      que.push(pair<pair<int, int>, int>(pair<int, int>(nx, ny), turn + 1));
    }
  }
  mi = inf;
  for (int i = (int)(0); i < (int)(h); i++) {
    mi = min(mi, tbl[i][0]);
    mi = min(mi, tbl[i][w - 1]);
  }
  for (int i = (int)(0); i < (int)(w); i++) {
    mi = min(mi, tbl[0][i]);
    mi = min(mi, tbl[h - 1][i]);
  }
  if (mi != inf) {
    cout << (mi + k - 1) / k << endl;
    return 0;
  }
}
