#include <bits/stdc++.h>
using namespace std;
char arena[810][810];
bool vis[810][810];
long long step[810][810];
long long w, h, k;
long long ax[] = {0, 0, -1, 1};
long long ay[] = {1, -1, 0, 0};
queue<pair<long long, long long> > q;
void bfs(long long x, long long y) {
  for (int i = 0; i < 4; i++) {
    long long newx = x + ax[i];
    long long newy = y + ay[i];
    if (vis[newx][newy] == 0 && newx >= 0 && newy >= 0 && newx < h &&
        newy < w) {
      step[newx][newy] += 1 + step[x][y];
      vis[newx][newy] = 1;
      q.push(make_pair(newx, newy));
    }
  }
}
int main() {
  memset(vis, 0, sizeof(vis));
  scanf("%lld %lld %lld", &w, &h, &k);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> arena[i][j];
      if (arena[i][j] == '#') {
        step[i][j] = 1;
      } else if (arena[i][j] == '.') {
        step[i][j] = 0;
      } else
        q.push(make_pair(i, j));
    }
  }
  vis[q.front().first][q.front().second] = 1;
  while (!q.empty()) {
    pair<long long, long long> temp = q.front();
    q.pop();
    bfs(temp.first, temp.second);
  }
  long long ans = k;
  for (int i = 0; i < w; i++) {
    ans = min(step[h - 1][i], ans);
    ans = min(step[0][i], ans);
  }
  for (int i = 0; i < h; i++) {
    ans = min(step[i][0], ans);
    ans = min(step[i][w - 1], ans);
  }
  printf("%lld\n", ans);
}
