#include <bits/stdc++.h>
using namespace std;
const int MAXN = 510;
const int INF = 0x3f3f3f3f;
int n, a[MAXN], d[MAXN][MAXN];
long long ans[MAXN];
bool f[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) scanf("%d", &d[i][j]);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (int k = n; k >= 1; k--) {
    int t = a[k];
    f[t] = 1;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (d[i][t] + d[t][j] < d[i][j]) d[i][j] = d[i][t] + d[t][j];
        if (f[i] && f[j]) ans[k] += d[i][j];
      }
  }
  for (int i = 1; i <= n; ++i) printf("%lld ", ans[i]);
  return 0;
}
