Problem p03546 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define init_random                                                            \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define start_routine                                                          \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  int begtime = clock();
#define end_routine                                                            \
  int endtime = clock();                                                       \
  cerr << endl                                                                 \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms";                                                               \
  return 0
#define contests_de_diya_kar 0
#define PB push_back
#define MP make_pair
#define int long long
#define sd(n) scanf("%lld", &n)
#define pd(n) printf("%lld\n", n)
#define MOD ((long long)(1000 * 1000 * 1000 + 7))
#define exit(a, b) return cout << a, b;
#define eps (double)1e-9
#define forn(a, b, i) for (int i = a; i < b; i += 1)
#define vi vector<int>
#define vb vector<bool>
#define all(v) v.begin(), v.end()
#define ll long long
#define pii pair<int, int>
#define mii map<int, int>
#define print(stuff) cout << stuff << endl
#define len(stuff) stuff.size()

const int N = (int)1e5 + 100;
int fac[N + 1], invfac[N + 1];
const int M = MOD;

int modexp(int a, int b) {
  if (b == 0)
    return 1;
  int ans = modexp(a, b / 2);
  ans = (ans * ans) % M;
  if (b % 2)
    ans = (ans * a) % M;
  return ans;
}

inline int inv(int a) { return modexp(a, M - 2); }

void prec() {
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = (fac[i - 1] * i) % M;
  invfac[N] = inv(fac[N]);
  for (int i = N - 1; i >= 0; i--)
    invfac[i] = (invfac[i + 1] * (i + 1)) % M;
}

int c(int n, int r) {
  int ans = fac[n];
  ans *= invfac[n - r], ans %= M;
  ans *= invfac[r], ans %= M;
  return ans;
}
const int inf = (int)1e15;

signed main() {
  start_routine;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  prec();

  int dist[10][10];
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      cin >> dist[i][j];

  for (int k = 0; k < 10; k++)
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      if (a == -1)
        continue;
      ans += dist[a][1];
    }
  }
  cout << ans;
  end_routine;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define init_random                                                            \
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define start_routine                                                          \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  int begtime = clock();
#define end_routine                                                            \
  int endtime = clock();                                                       \
  cerr << endl                                                                 \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms";                                                               \
  return 0
#define contests_de_diya_kar 0
#define PB push_back
#define MP make_pair
#define int long long
#define sd(n) scanf("%lld", &n)
#define pd(n) printf("%lld\n", n)
#define MOD ((long long)(1000 * 1000 * 1000 + 7))
#define exit(a, b) return cout << a, b;
#define eps (double)1e-9
#define forn(a, b, i) for (int i = a; i < b; i += 1)
#define vi vector<int>
#define vb vector<bool>
#define all(v) v.begin(), v.end()
#define ll long long
#define pii pair<int, int>
#define mii map<int, int>
#define print(stuff) cout << stuff << endl
#define len(stuff) stuff.size()

const int N = (int)1e5 + 100;
int fac[N + 1], invfac[N + 1];
const int M = MOD;

int modexp(int a, int b) {
  if (b == 0)
    return 1;
  int ans = modexp(a, b / 2);
  ans = (ans * ans) % M;
  if (b % 2)
    ans = (ans * a) % M;
  return ans;
}

inline int inv(int a) { return modexp(a, M - 2); }

void prec() {
  fac[0] = 1;
  for (int i = 1; i <= N; i++)
    fac[i] = (fac[i - 1] * i) % M;
  invfac[N] = inv(fac[N]);
  for (int i = N - 1; i >= 0; i--)
    invfac[i] = (invfac[i + 1] * (i + 1)) % M;
}

int c(int n, int r) {
  int ans = fac[n];
  ans *= invfac[n - r], ans %= M;
  ans *= invfac[r], ans %= M;
  return ans;
}
const int inf = (int)1e15;

signed main() {
  start_routine;
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
#endif
  prec();

  int dist[10][10];
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      cin >> dist[i][j];

  for (int k = 0; k < 10; k++)
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      if (a == -1)
        continue;
      ans += dist[a][1];
    }
  }
  cout << ans;
  end_routine;
}