#include <bits/stdc++.h>
using namespace std;
struct p {
  int x, y;
  p(int a = 0, int b = 0) : x(a), y(b) {}
};
int n, m, k;
char s[805][805];
p prv[805][805];
int dist[805][805];
int distlock[805][805];
p que[800005];
int l, r;
const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  int sx = 0, sy = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
        s[i][j] = '.';
      }
    }
  que[r++] = p(sx, sy);
  memset(dist, -1, sizeof(dist));
  dist[sx][sy] = 0;
  while (l < r) {
    p nw = que[l++];
    int x = nw.x, y = nw.y;
    for (int k = 0; k < 4; k++) {
      int nx = x + dx[k], ny = y + dy[k];
      int nwdist = dist[x][y] + 1;
      int nwdistlock = distlock[x][y] + (s[nx][ny] == '#' ? 1 : 0);
      if (!~dist[nx][ny]) {
        dist[nx][ny] = nwdist;
        distlock[nx][ny] = nwdistlock;
        prv[nx][ny] = p(x, y);
        que[r++] = p(nx, ny);
      }
      if (dist[nx][ny] == nwdist && distlock[nx][ny] > nwdistlock) {
        distlock[nx][ny] = nwdistlock;
        prv[nx][ny] = p(x, y);
        que[r++] = p(nx, ny);
      }
    }
  }
  p out(1, 1);
  for (int i = 1; i <= n; i++) {
    int ox = out.x, oy = out.y;
    int x = i, y = 1;
    if (dist[x][y] < dist[ox][oy]) out = p(x, y);
    if (dist[x][y] == dist[ox][oy] && distlock[x][y] < distlock[ox][oy])
      out = p(x, y);
    x = i, y = m;
    ox = out.x, oy = out.y;
    if (dist[x][y] < dist[ox][oy]) out = p(x, y);
    if (dist[x][y] == dist[ox][oy] && distlock[x][y] < distlock[ox][oy])
      out = p(x, y);
  }
  for (int i = 1; i <= m; i++) {
    int ox = out.x, oy = out.y;
    int x = 1, y = i;
    if (dist[x][y] < dist[ox][oy]) out = p(x, y);
    if (dist[x][y] == dist[ox][oy] && distlock[x][y] < distlock[ox][oy])
      out = p(x, y);
    x = n, y = i;
    ox = out.x, oy = out.y;
    if (dist[x][y] < dist[ox][oy]) out = p(x, y);
    if (dist[x][y] == dist[ox][oy] && distlock[x][y] < distlock[ox][oy])
      out = p(x, y);
  }
  vector<p> chain;
  p nd;
  for (nd = out; nd.x != sx || nd.y != sy; nd = prv[nd.x][nd.y])
    chain.push_back(nd);
  chain.push_back(nd);
  reverse(chain.begin(), chain.end());
  int ans = 0;
  for (int i = 0; i < (int)chain.size();) {
    int j;
    for (j = i; j < (int)chain.size() && s[chain[j].x][chain[j].y] != '#' &&
                j - i + 1 <= k;
         j++)
      ;
    i = j;
    for (int cnt = 0; j < (int)chain.size() && cnt <= k; j++)
      if (s[chain[j].x][chain[j].y] == '#') {
        s[chain[j].x][chain[j].y] = '.';
        cnt++;
      }
    ans++;
  }
  printf("%d\n", ans);
  return 0;
}
