#include <bits/stdc++.h>
using namespace std;
long long matrix[505][505];
int x[505];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &matrix[i][j]);
    }
  }
  for (int i = 1; i <= n; i++) scanf("%d", &x[i]);
  reverse(x + 1, x + n + 1);
  stack<long long> ans;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        matrix[x[j]][x[k]] =
            min(matrix[x[j]][x[k]], matrix[x[j]][x[i]] + matrix[x[i]][x[k]]);
      }
    }
    long long res = 0;
    for (int j = 1; j <= i; j++) {
      for (int k = 1; k <= i; k++) {
        res += matrix[x[j]][x[k]];
      }
    }
    ans.push(res);
  }
  while (!ans.empty()) {
    printf("%lld ", ans.top());
    ans.pop();
  }
  printf("\n");
}
