#include <bits/stdc++.h>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 8e2 + 5, dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, m, k, d[N][N], ans;
bool vis[N][N];
char s[N][N];
deque<pair<int, int> > q;
int main() {
  n = read(), m = read(), k = read(), ans = n * m;
  for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j)
      if (s[i][j] == 'S') q.push_back(make_pair(i, j)), vis[i][j] = true;
  while (q.size()) {
    int x = q.front().first, y = q.front().second;
    q.pop_front();
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx > n || nx <= 0 || ny > m || ny <= 0 || vis[nx][ny]) continue;
      vis[nx][ny] = true;
      d[nx][ny] = d[x][y] + (s[nx][ny] == '#');
      if (s[nx][ny] == '#')
        q.push_back(make_pair(nx, ny));
      else
        q.push_front(make_pair(nx, ny));
    }
  }
  for (int i = 1; i <= n; ++i)
    ans = min(ans, (int)ceil(1.0 * d[i][m] / k) + 1),
    ans = min(ans, (int)ceil(1.0 * d[i][1] / k) + 1);
  for (int i = 1; i <= m; ++i)
    ans = min(ans, (int)ceil(1.0 * d[n][i] / k) + 1),
    ans = min(ans, (int)ceil(1.0 * d[1][i] / k) + 1);
  printf("%d\n", ans);
  return 0;
}
