#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30) - 1;
const long double EPS = 1e-9;
const long double PI = fabs(atan2(0.0, -1.0));
int n;
long long a[510][510];
int order[510];
void load() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) scanf("%lld", &a[i][j]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &order[i]);
    --order[i];
  }
}
long long answer[510];
bool alive[510];
void solve() {
  memset(alive, false, sizeof(alive));
  for (int it = n - 1; it >= 0; it--) {
    int i = order[it];
    alive[i] = true;
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        a[j][k] = min(a[j][k], a[j][i] + a[i][k]);
      }
    }
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (alive[j] && alive[k] && j != k) answer[it] += a[j][k];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (i > 0) printf(" ");
    printf("%lld", answer[i]);
  }
}
int main() {
  load();
  solve();
  return 0;
}
