#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)8e2 + 5;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
vector<pair<int, int> > mem[MAXN * 3];
bool vis[MAXN][MAXN];
char s[MAXN][MAXN];
int d[MAXN][MAXN];
int n, m, k, ans;
void calcD(int stx, int sty) {
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      d[i][j] = -1;
    }
  }
  deque<pair<int, int> > S;
  S.push_back(make_pair(stx, sty));
  d[stx][sty] = 0;
  while (!S.empty()) {
    int x = S.front().first, y = S.front().second;
    S.pop_front();
    for (int dir = 0; dir < 4; ++dir) {
      int nx = x + dx[dir];
      int ny = y + dy[dir];
      if (nx < 1 || nx > n || ny < 1 || ny > m) {
        continue;
      }
      if (d[nx][ny] == -1) {
        if (s[nx][ny] == '#') {
          d[nx][ny] = d[x][y] + 1;
          S.push_back(make_pair(nx, ny));
        } else {
          d[nx][ny] = d[x][y];
          S.push_front(make_pair(nx, ny));
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (s[i][j] == '#') {
        mem[d[i][j]].push_back(make_pair(i, j));
      }
    }
  }
}
void solve() {
  scanf("%d %d %d", &n, &m, &k);
  int stx, sty;
  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') {
        stx = i;
        sty = j;
      }
    }
  }
  calcD(stx, sty);
  vector<pair<int, int> > vv;
  vv.push_back(make_pair(stx, sty));
  vis[stx][sty] = 1;
  while (1) {
    queue<pair<pair<int, int>, int> > q;
    ++ans;
    for (auto it : vv) {
      q.push(make_pair(make_pair(it.first, it.second), 0));
    }
    vv.clear();
    while (!q.empty()) {
      int x = q.front().first.first;
      int y = q.front().first.second;
      int cur = q.front().second;
      q.pop();
      if (x == 1 || x == n || y == 1 || y == m) {
        printf("%d\n", ans);
        return;
      }
      if (cur == k) {
        vv.push_back(make_pair(x, y));
        break;
      }
      vis[x][y] = 1;
      for (int dir = 0; dir < 4; ++dir) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (nx < 1 || nx > n || ny < 1 || ny > m) {
          continue;
        }
        if (s[nx][ny] != '#') {
          if (!vis[nx][ny]) {
            vis[nx][ny] = 1;
            q.push(make_pair(make_pair(nx, ny), cur + 1));
          }
        }
      }
    }
    for (int i = (ans - 1) * k + 1; i <= ans * k; ++i) {
      for (auto it : mem[i]) {
        s[it.first][it.second] = '.';
        for (int dir = 0; dir < 4; ++dir) {
          int x = it.first + dx[dir];
          int y = it.second + dy[dir];
          if (x < 1 || x > n || y < 1 || y > m) {
            continue;
          }
          if (vis[x][y]) {
            vv.push_back(make_pair(x, y));
          }
        }
      }
    }
  }
  printf("%d\n", ans);
}
int main() {
  int tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}
