#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 505, INF = 0x3f3f3f3f;
long long n, mtrx[MAXN][MAXN], minDist[MAXN][MAXN], x[MAXN], ans[MAXN],
    flg[MAXN];
signed main() {
  memset(minDist, INF, sizeof minDist);
  scanf("%lld", &n);
  for (long long i = 1; i <= n; ++i)
    for (long long j = 1; j <= n; ++j) scanf("%lld", &mtrx[i][j]);
  for (long long i = 1; i <= n; ++i) scanf("%lld", &x[i]);
  for (long long i = n; i >= 1; --i) {
    flg[x[i]] = true;
    for (long long t = 1; t <= 2; ++t)
      for (long long j = 1; j <= n; ++j) {
        if (!flg[j]) continue;
        minDist[j][x[i]] = mtrx[j][x[i]];
        minDist[x[i]][j] = mtrx[x[i]][j];
        for (long long k = 1; k <= n; ++k)
          if (flg[k]) {
            minDist[j][x[i]] =
                min(minDist[j][x[i]], minDist[j][k] + minDist[k][x[i]]);
            minDist[x[i]][j] =
                min(minDist[x[i]][j], minDist[x[i]][k] + minDist[k][j]);
          }
      }
    for (long long j = 1; j <= n; ++j) {
      if (!flg[j]) continue;
      for (long long k = 1; k <= n; ++k)
        if (flg[k])
          minDist[j][k] =
              min(minDist[j][k], minDist[j][x[i]] + minDist[x[i]][k]);
    }
    for (long long j = 1; j <= n; ++j)
      for (long long k = 1; k <= n; ++k)
        if (k != j && flg[j] && flg[k]) ans[i] += minDist[j][k];
  }
  for (long long i = 1; i <= n; ++i) printf("%lld ", ans[i]);
}
