alexjercan/bugnet huggingface dataset:
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char field[1000][1000];
bool flg[1000][1000] = {{0}};

class Data {
public:
  int x, y, count;
  Data() {}
  Data(int x, int y, int count) : x(x), y(y), count(count) {}
};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  Data start;
  int ans = 0;
  for (int i = 0; i < h; i++) {
    cin >> field[i];
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        start = Data(j, i, 0);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    queue<Data> Q;
    Q.push(start);
    memset(flg, 0, sizeof(flg));
    while (!Q.empty()) {
      Data d = Q.front();
      Q.pop();
      if (field[d.y][d.x] == i + '0') {
        start = Data(d.x, d.y, d.count);
        break;
      }
      if (flg[d.y][d.x])
        continue;
      flg[d.y][d.x] = true;
      for (int j = 0; j < 4; j++) {
        int px = d.x + dx[j], py = d.y + dy[j];
        if (px < 0 || py < 0 || px >= w || py >= h)
          continue;
        if (field[py][px] == 'X')
          continue;
        Q.push(Data(px, py, d.count + 1));
      }
    }
    ans = start.count;
  }

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char field[1000][1000];

class Data {
public:
  int x, y, count;
  Data() {}
  Data(int x, int y, int count) : x(x), y(y), count(count) {}
};
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  Data start;
  int ans = 0;
  for (int i = 0; i < h; i++) {
    cin >> field[i];
    for (int j = 0; j < w; j++) {
      if (field[i][j] == 'S') {
        start = Data(j, i, 0);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    queue<Data> Q;
    Q.push(start);
    // bool flg[1000][1000] = {{0}};
    while (!Q.empty()) {
      Data d = Q.front();
      Q.pop();
      if (field[d.y][d.x] == i + '0') {
        start = Data(d.x, d.y, d.count);
        break;
      }
      // if (flg[d.y][d.x]) continue;
      // flg[d.y][d.x] = true;
      for (int j = 0; j < 4; j++) {
        int px = d.x + dx[j], py = d.y + dy[j];
        if (px < 0 || py < 0 || px >= w || py >= h)
          continue;
        if (field[py][px] == 'X')
          continue;
        Q.push(Data(px, py, d.count + 1));
      }
    }
    ans = start.count;
  }

  cout << ans << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#define x first
#define y second
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int h, w, k;
pair<int, int> p[12];
bool t[1010][1010], vis[1010][1010];
queue<pair<int, pair<int, int>>> Q;
int main() {
  cin >> h >> w >> k;
  rep(i, 0, h) rep(j, 0, w) {
    t[i][j] = true;
    char z;
    cin >> z;
    if (z == 'X')
      t[i][j] = 0;
    if (z == 'S')
      p[0] = make_pair(i, j);
    if (z > '0' && z <= '9')
      p[z - '0'] = make_pair(i, j);
  }
  int a = 0;
  rep(r, 0, k) {
    Q.push(make_pair(0, p[r]));
    rep(i, 0, h) rep(j, 0, w) vis[i][j] = 0;
    vis[p[r].x][p[r].y] = 1;
    while (1) {
      int L = Q.front().x, Y = Q.front().y.x, X = Q.front().y.y;
      if (Y == p[r + 1].x && X == p[r + 1].y) {
        a += L;
        break;
      }
      int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
      rep(i, 0, 4) {
        int f = Y + dy[i], g = X + dx[i];
        if (f >= 0 && f < h && g >= 0 && g < w) {
          if (t[f][g] == 1 && vis[f][g] == 0) {
            vis[f][g] = 1;
            Q.push(make_pair(L + 1, make_pair(f, g)));
          }
        }
      }
      Q.pop();
    }
    while (!Q.empty())
      Q.pop();
  }
  cout << a << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#define x first
#define y second
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int h, w, k;
pair<int, int> p[12];
bool t[1010][1010], vis[1010][1010];
queue<pair<int, pair<int, int>>> Q;
int main() {
  cin >> h >> w >> k;
  rep(i, 0, h) rep(j, 0, w) {
    t[i][j] = true;
    char z;
    cin >> z;
    if (z == 'X')
      t[i][j] = 0;
    if (z == 'S')
      p[0] = make_pair(i, j);
    if (z > '0' && z <= '9')
      p[z - '0'] = make_pair(i, j);
  }
  int a = 0;
  rep(r, 0, k) {
    Q.push(make_pair(0, p[r]));
    rep(i, 0, h) rep(j, 0, w) vis[i][j] = 0;
    vis[p[r].x][p[r].y] = 1;
    while (1) {
      int L = Q.front().x, Y = Q.front().y.x, X = Q.front().y.y;
      if (Y == p[r + 1].x && X == p[r + 1].y) {
        a += L;
        break;
      }
      int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
      rep(i, 0, 4) {
        int f = Y + dy[i], g = X + dx[i];
        if (f >= 0 && f < h && g >= 0 && g < w) {
          if (t[f][g] == 1 && vis[f][g] == 0) {
            vis[Y][X] = 1;
            Q.push(make_pair(L + 1, make_pair(f, g)));
          }
        }
      }
      Q.pop();
    }
    while (!Q.empty())
      Q.pop();
  }
  cout << a << endl;
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;
#define MAX 1000
struct Point {
  int x, y, dist;
};
int main() {
  int H, W, N;
  int vital;
  int i, j;
  char field[MAX][MAX];
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {-1, 0, 1, 0};
  int sx, sy, ans;

  cin >> H >> W >> N;
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      cin >> field[i][j];
      if (field[i][j] == 'S') {
        sy = i;
        sx = j;
        ans = 0;
      }
    }
  }

  for (vital = 1; vital <= N; vital++) {
    queue<Point> q;
    bool visited[MAX][MAX] = {0};

    Point p = {sx, sy, 0}, p2;
    q.push(p);
    while (1) {
      p = q.front();
      q.pop();

      if (field[p.y][p.x] == (vital + '0')) {
        sy = p.y;
        sx = p.x;
        ans += p.dist;
        break;
      }

      for (i = 0; i < 4; i++) {
        p2.y = p.y + dy[i];
        p2.x = p.x + dx[i];
        p2.dist = p.dist + 1;
        if (p2.y < 0 || H <= p2.y)
          continue;
        if (p2.x < 0 || W <= p2.x)
          continue;
        if (field[p2.y][p2.x] == 'X')
          continue;
        if (visited[p2.y][p2.x])
          continue;
        visited[p2.y][p2.x] = true;
        q.push(p2);
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
struct Point {
  int x, y, dist;
};
int main() {
  int H, W, N;
  int vital;
  int i, j;
  string field[1000];
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {-1, 0, 1, 0};
  int sx, sy, ans;
  bool b;

  cin >> H >> W >> N;
  for (i = 0; i < H; i++)
    cin >> field[i];
  b = 1;
  for (i = 0; b && i < H; i++) {
    for (j = 0; b && j < W; j++) {
      if (field[i][j] == 'S') {
        sy = i;
        sx = j;
        ans = 0;
        b = 0;
      }
    }
  }

  for (vital = 1; vital <= N; vital++) {
    queue<Point> q;
    bool visited[1000][1000] = {0};
    bool f = 1;

    Point p = {sx, sy, 0};
    q.push(p);
    while (f) {
      Point p = q.front();
      q.pop();
      visited[p.y][p.x] = true;

      for (i = 0; f & i < 4; i++) {
        Point p2;
        p2.y = p.y + dy[i];
        p2.x = p.x + dx[i];
        p2.dist = p.dist + 1;
        if (p2.y < 0 || H <= p2.y)
          continue;
        if (p2.x < 0 || W <= p2.x)
          continue;
        if (field[p2.y][p2.x] == 'X')
          continue;
        if (visited[p2.y][p2.x])
          continue;
        if (field[p2.y][p2.x] == (vital + '0')) {
          sy = p2.y;
          sx = p2.x;
          ans += p2.dist;
          f = 0;
        }
        q.push(p2);
      }
    }
  }
  cout << ans << endl;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = 100000000;
typedef pair<int, int> P;
int sx, sy;

char _map[1000][1001];
int H, W;
char N;
char n = '1';

int d[1000][1000];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void init() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      d[i][j] = INF;
}

int bfs() {
  int ans = 0;
  queue<P> que;
  init();
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (que.size()) {
    P p = que.front();
    que.pop();
    if (_map[p.first][p.second] == n) {
      if (n == N) {
        ans += d[p.first][p.second];
        break;
      } else {
        ans += d[p.first][p.second];
        init();
        queue<P> empty;
        swap(que, empty);
        sx = p.first;
        sy = p.second;
        d[sx][sy] = 0;
        n++;
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];

      if (0 <= nx && nx < H && 0 <= ny && ny < W && _map[nx][ny] != 'X' &&
          d[nx][ny] == INF) {
        que.push(P(nx, ny));
        d[nx][ny] = d[p.first][p.second] + 1;
      }
    }
  }
  return ans;
}

void solve() {
  int res = bfs();
  cout << res << endl;
}

int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> _map[i][j];
      if (_map[i][j] == 'S') {
        sx = i;
        sy = j;
      }
    }
  }
  solve();
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdlib.h>
#include <vector>

using namespace std;

int H, W;
char N;
vector<vector<char>> _map(H, vector<char>(W));
int num = 1;

int solve(int i, int j, int c) {
  if (_map[i][j] == N)
    return c;
  for (int dx = -1; dx <= 1; dx++) {
    for (int dy = -1; dy <= 1; dy++) {
      if (dx != dy && dx != -1 * dy && (j + dx < W && i + dy < H)) {
        int tmp;
        tmp = static_cast<char>(num);
        if (_map[i + dy][j + dx] == tmp || _map[i + dy][j + dx] == '.') {
          num++;
          solve(i + dy, j + dx, c + 1);
        }
      }
    }
  }
  return 0;
}

int main() {
  cin >> H >> W >> N;
  int x, y;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> _map[i][j];
      if (_map[i][j] == 'S') {
        x = j;
        y = i;
      }
    }
  }
  cout << solve(y, x, 0) << endl;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

int mo[4] = {0, 1, 0, -1};

int main() {
  int h, w, n, ans = 0;
  bool fl[1000][1000] = {0};
  char in[1000][1001];
  queue<pair<int, int>> qp;
  pair<int, int> p;

  scanf("%d%d%d", &h, &w, &n);

  for (int i = 0; i < h; i++)
    scanf("%s", in[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (in[i][j] == 'S') {
        qp.push(make_pair(i, j));
        qp.push(make_pair(-5, -5));
        break;
      }

  for (int i = 1; i <= n; i++) {
    while (1) {
      p = qp.front();
      qp.pop();
      if (p.first == -5 && p.second == -5) {
        ans++;
        qp.push(make_pair(-5, -5));
        continue;
      }
      if (p.first < 0 || h <= p.first || p.second < 0 || w <= p.second ||
          fl[p.first][p.second])
        continue;
      if (in[p.first][p.second] == '0' + i)
        break;
      if (in[p.first][p.second] == 'X')
        continue;

      fl[p.first][p.second] = true;

      for (int j = 0; j < 4; j++)
        qp.push(make_pair(p.first + mo[j], p.second + mo[(j + 1) % 4]));
    }
    while (!qp.empty())
      qp.pop();
    qp.push(make_pair(p.first, p.second));
    qp.push(make_pair(-5, -5));

    for (int k = 0; k < h; k++)
      for (int j = 0; j < w; j++)
        fl[k][j] = false;
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

int mo[4] = {0, 1, 0, -1};

int main() {
  int h, w, n, ans = 0;
  bool fl[1000][1000] = {0};
  char in[1000][1001];
  queue<pair<int, int>> qp;
  pair<int, int> p;

  scanf("%d%d%d", &h, &w, &n);

  for (int i = 0; i < h; i++)
    scanf("%s", in[i]);

  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      if (in[i][j] == 'S') {
        qp.push(make_pair(i, j));
        qp.push(make_pair(-5, -5));
        break;
      }

  for (int i = 1; i <= n; i++) {
    while (1) {
      p = qp.front();
      qp.pop();
      if (p.first == -5 && p.second == -5) {
        ans++;
        qp.push(make_pair(-5, -5));
        continue;
      }
      if (p.first < 0 || h <= p.first || p.second < 0 || w <= p.second ||
          fl[p.first][p.second])
        continue;
      if (in[p.first][p.second] == '0' + i)
        break;
      if (in[p.first][p.second] == 'X')
        continue;

      for (int j = 0; j < 4; j++)
        qp.push(make_pair(p.first + mo[j], p.second + mo[(j + 1) % 4]));
    }
    while (!qp.empty())
      qp.pop();
    qp.push(make_pair(p.first, p.second));
    qp.push(make_pair(-5, -5));
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define INF 10000000
#define rep(i, s, t) for (int i = s; i <= (t); i++)
#define MP(a, b) make_pair(a, b)
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, n;
char m[1002][1002];

int bfs(int s) {
  static int d[1002][1002];
  queue<pair<int, int>> q;
  pair<int, int> a, b;
  rep(i, 1, h) rep(j, 1, w) if (m[i][j] == '0' + s) {
    d[i][j] = 0;
    q.push(MP(i, j));
  }
  else d[i][j] = INF;
  while (1) {
    a = q.front();
    q.pop();
    rep(k, 0, 3) {
      b = MP(a.first + dy[k], a.second + dx[k]);
      if (m[b.first][b.second] == 'X')
        continue;
      if (d[a.first][a.second] + 1 < d[b.first][b.second]) {
        d[b.first][b.second] = d[a.first][a.second] + 1;
        if (m[b.first][b.second] == '0' + s + 1)
          return d[b.first][b.second];
        q.push(b);
      }
    }
  }
  return -1;
}
int main() {
  cin >> h >> w >> n;
  memset(m, 'X', sizeof(m));
  rep(i, 1, h) rep(j, 1, w) {
    cin >> m[i][j];
    if (m[i][j] == 'S')
      m[i][j] = '0';
  }
  int ans = 0;
  rep(i, 0, n - 1) ans += bfs(i);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
#define INF 10000000
#define rep(i, s, t) for (int i = s; i <= (t); i++)
#define MP(a, b) make_pair(a, b)
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int h, w, n;
char m[1000][1000];

int bfs(int s) {
  static int d[1000][1000];
  queue<pair<int, int>> q;
  pair<int, int> a, b;
  rep(i, 1, h) rep(j, 1, w) if (m[i][j] == '0' + s) {
    d[i][j] = 0;
    q.push(MP(i, j));
  }
  else d[i][j] = INF;
  while (1) {
    a = q.front();
    q.pop();
    rep(k, 0, 3) {
      b = MP(a.first + dy[k], a.second + dx[k]);
      if (m[b.first][b.second] == 'X')
        continue;
      if (d[a.first][a.second] + 1 < d[b.first][b.second]) {
        d[b.first][b.second] = d[a.first][a.second] + 1;
        if (m[b.first][b.second] == '0' + s + 1)
          return d[b.first][b.second];
        q.push(b);
      }
    }
  }
  return -1;
}
int main() {
  cin >> h >> w >> n;
  memset(m, 'X', sizeof(m));
  rep(i, 1, h) rep(j, 1, w) {
    cin >> m[i][j];
    if (m[i][j] == 'S')
      m[i][j] = '0';
  }
  int ans = 0;
  rep(i, 0, n - 1) ans += bfs(i);
  cout << ans << endl;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
#define MAX 1005

int h, w, n;
char city[MAX][MAX];
struct node {
  int x, y, time;
} start;
int direc1[4] = {-1, 0, 1, 0}, direc2[4] = {0, 1, 0, -1};
map<int, int> visited;

void dfs(char end) {
  queue<node> que;
  que.push(start);
  visited[start.x * w + start.y] = 1;
  int nextx, nexty;
  while (que.size() != 0) {
    node now = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      nextx = now.x + direc1[i];
      nexty = now.y + direc2[i];
      if (nextx >= 0 && nextx < h && nexty >= 0 && nexty < w &&
          city[nextx][nexty] != 'X' && !visited[nextx * w + nexty]) {

        if (city[nextx][nexty] == end) {
          start.x = nextx;
          start.y = nexty;
          start.time = now.time + 1;
          return;
        } else {
          visited[nextx * w + nexty] = 1;
          node now1;
          now1.x = nextx;
          now1.y = nexty;
          now1.time = now.time + 1;
          que.push(now1);
        }
      }
    }
  }
}

int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> city[i][j];
      if (city[i][j] == 'S') {
        start.x = i;
        start.y = j;
        start.time = 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    visited.clear();
    dfs(i + '1');
  }
  cout << start.time << endl;
  // system("pause");
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
using namespace std;

char ma[1005][1005];
int vis[1005][1005];
int w, h, n;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

struct node {
  int x, y, time;
};
node start;

void bfs(char ed) {
  queue<node> que;
  que.push(start);
  vis[start.x][start.y] = 1;
  while (!que.empty()) {
    node no = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int x = no.x + dx[i];
      int y = no.y + dy[i];
      if (vis[x][y] || x >= h || x < 0 || y >= w || y < 0 || ma[x][y] == 'X')
        continue;
      if (ma[x][y] == ed) {
        start.x = x;
        start.y = y;
        start.time = no.time + 1;
        return;
      }
      vis[x][y] = 1;
      node no1;
      no1.x = x;
      no1.y = y;
      no1.time = no.time + 1;
      que.push(no1);
    }
  }
}

int main() {
  while (scanf("%d %d %d\\n", &h, &w, &n)) {
    for (int i = 0; i < h; i++) {
      scanf("%s", &ma[i]);
      for (int j = 0; j < w; j++)
        if (ma[i][j] == 'S') {
          start.x = i;
          start.y = j;
          start.time = 0;
        }
    }
    for (int i = 0; i < n; i++) {
      memset(vis, 0, sizeof(vis));
      bfs(i + '1');
    }

    printf("%d\\n", start.time);
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>

#define rep(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second

using namespace std;

char m[1000][1000];
int dis[1000][1000];
queue<pair<int, int>> state;

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

  cin >> h >> w >> n;

  int sx, sy;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> m[j][i];
      if (m[j][i] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }

  pair<int, int> p;
  p.ft = sx;
  p.sd = sy;

  rep(i, 0, h) {
    rep(j, 0, w) { dis[j][i] = -1; }
  }
  dis[sx][sy] = 0;
  state.push(p);

  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  int map2[1000][1000];
  for (int lp = 1; lp <= n; lp++) {
    rep(i, 0, h) {
      rep(j, 0, w) { map2[j][i] = 0; }
    }

    map2[sx][sy] = 1;
    while (state.size()) {
      pair<int, int> now = state.front();
      state.pop();
      if (m[now.ft][now.sd] - '0' == lp) {
        sx = now.ft;
        sy = now.sd;
        break;
      }

      rep(lp2, 0, 4) {
        int x = now.ft + dx[lp2];
        int y = now.sd + dy[lp2];

        if (x < 0 || w <= x || y < 0 || h <= y)
          continue;
        if (map2[x][y] == 0 && m[x][y] != 'X') {
          state.push(make_pair(x, y));
          dis[x][y] = dis[now.ft][now.sd] + 1;
          map2[x][y] = 1;
        }
      }
    }
    while (state.size())
      state.pop();

    state.push(make_pair(sx, sy));
  }

  cout << dis[sx][sy] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>

#define rep(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define ft first
#define sd second

using namespace std;

char m[1000][1000];
int dis[1000][1000];
queue<pair<int, int>> state;

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

  cin >> h >> w >> n;

  int sx, sy;
  rep(i, 0, h) {
    rep(j, 0, w) {
      cin >> m[j][i];
      if (m[j][i] == 'S') {
        sx = j;
        sy = i;
      }
    }
  }

  pair<int, int> p;
  p.ft = sx;
  p.sd = sy;

  rep(i, 0, h) {
    rep(j, 0, w) { dis[j][i] = -1; }
  }
  dis[sx][sy] = 0;
  state.push(p);

  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  int map2[1000][1000];
  for (int lp = 1; lp <= n; lp++) {
    rep(i, 0, h) {
      rep(j, 0, w) { map2[j][i] = 0; }
    }

    map2[sx][sy] = 1;
    while (state.size()) {
      pair<int, int> now = state.front();
      state.pop();
      if (m[now.ft][now.sd] - '0' == lp) {
        sx = now.ft;
        sy = now.sd;
        break;
      }

      rep(lp2, 0, 4) {
        int x = now.ft + dx[lp2];
        int y = now.sd + dy[lp2];

        if (x < 0 || w <= x || y < 0 || h <= y)
          continue;
        if (map2[x][y] == 0 && m[x][y] != 'X') {
          state.push(make_pair(x, y));
          dis[x][y] = dis[now.ft][now.sd] + 1;
          map2[now.ft][now.sd] = 1;
        }
      }
    }
    while (state.size())
      state.pop();

    state.push(make_pair(sx, sy));
  }

  cout << dis[sx][sy] << endl;
  return 0;
}