#include <bits/stdc++.h>
using namespace std;
int n, m, k;
char ch[807][807];
int dx[4] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int dis[807][807];
void dij(int a, int b) {
  priority_queue<pair<int, pair<int, int> >,
                 vector<pair<int, pair<int, int> > >,
                 greater<pair<int, pair<int, int> > > >
      q;
  q.push(make_pair(0, make_pair(a, b)));
  memset((void*)dis, 0x7f, sizeof(dis));
  dis[a][b] = 0;
  while (!q.empty()) {
    int xa = q.top().second.first, xb = q.top().second.second,
        qb = q.top().first;
    q.pop();
    if (dis[xa][xb] != qb) continue;
    for (int i = 0; i < 4; i++) {
      int xx = xa + dx[i], yy = xb + dy[i];
      if (xx >= 0 && xx < n && yy >= 0 && yy < m) {
        int disj = (ch[xx][yy] == '#');
        if (dis[xa][xb] + disj < dis[xx][yy]) {
          dis[xx][yy] = dis[xa][xb] + disj;
          q.push(make_pair(dis[xx][yy], make_pair(xx, yy)));
        }
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) scanf("%s", ch[i]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (ch[i][j] == 'S') dij(i, j);
  int minn = ((int)0x7f7f7f7f);
  for (int i = 0; i < n; i++) {
    if (dis[i][0] < minn) minn = dis[i][0];
    if (dis[i][m - 1] < minn) minn = dis[i][m - 1];
  }
  for (int i = 0; i < m; i++) {
    if (dis[0][i] < minn) minn = dis[0][i];
    if (dis[n - 1][i] < minn) minn = dis[n - 1][i];
  }
  printf("%d\n", (minn + k - 1) / k + 1);
  return 0;
}
