/*
 * h.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_H = 10;
const int MAX_W = 10;
const int MAX_N = 9;
const int M = 5;
const int DBITS = 1 << (M * 2);

const int INF = 1 << 30;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

struct Stat {
  int d, k, x, y, dbits;
};

/* global variables */

string flds[MAX_H];
int xs[M], ys[M];
int dists[MAX_N][MAX_H][MAX_W][DBITS];

/* subroutines */

bool move(Stat &s, int h, int w, int di) {
  int x0 = s.x + dxs[di], y0 = s.y + dys[di];
  if (x0 < 0 || x0 >= w || y0 < 0 || y0 >= h || flds[y0][x0] == '#')
    return false;
  for (int i = 0, x = s.x, y = s.y; i < M - 1; i++) {
    int dii = ((s.dbits >> (i * 2)) & 3);
    x += dxs[dii], y += dys[dii];
    if (x0 == x && y0 == y) return false;
  }

  s.dbits = ((s.dbits << 2) & (DBITS - 1)) | ((di + 2) & 3);
  s.x = x0, s.y = y0;
  s.d++;
  return true;
}

/* main */

int main() {
  int h, w, n;
  cin >> h >> w >> n;

  Stat st;
  st.d = st.k = st.dbits = 0;

  for (int y = 0; y < h; y++) {
    cin >> flds[y];
    for (int x = 0; x < w; x++) {
      char ch = flds[y][x];
      if (ch == 'S') st.x = x, st.y = y;
      else if (ch >= 'a' && ch <= 'e') {
	int id = ch - 'a';
	xs[id] = x, ys[id] = y;
      }
    }
  }

  for (int i = 0, x = st.x, y = st.y; i < M; i++) {
    int dx = xs[i] - x, dy = ys[i] - y;
    for (int di = 0; di < 4; di++)
      if (dx == dxs[di] && dy == dys[di]) {
	st.dbits |= (di << (i * 2));
	break;
      }
    x = xs[i], y = ys[i];
  }
  
  for (int i = 0; i < n; i++)
    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++)
	for (int bits = 0; bits < DBITS; bits++)
	  dists[i][y][x][bits] = INF;
  dists[0][st.y][st.x][st.dbits] = 0;

  queue<Stat> q;
  q.push(st);
  int ans = -1;
  
  while (! q.empty()) {
    Stat u = q.front(); q.pop();

    int uch = flds[u.y][u.x];
    if (uch == '1' + u.k) {
      if (++u.k >= n) {
	ans = u.d;
	break;
      }
      dists[u.k][u.y][u.x][u.dbits] = u.d;
    }

    for (int di = 0; di < 4; di++) {
      Stat v = u;
      if (move(v, h, w, di) &&
	  dists[v.k][v.y][v.x][v.dbits] > v.d) {
	dists[v.k][v.y][v.x][v.dbits] = v.d;
	q.push(v);
      }
    }
  }

  printf("%d\n", ans);
  return 0;
}
