#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3fffffff;
int w1[505][505];
int w2[505][505];
int rod[505];
int od[505];
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &w1[i][j]);
      w2[i][j] = INF;
    }
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &rod[i]);
    od[n + 1 - i] = rod[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      w2[i][j] = w1[od[i]][od[j]];
    }
  }
  stack<long long> s;
  for (int k = 1; k <= n; k++) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        w2[i][j] = min(w2[i][j], w2[i][k] + w2[k][j]);
      }
    }
    for (int i = 1; i <= k; i++) {
      for (int j = 1; j <= k; j++) {
        ans += (long long)w2[i][j];
      }
    }
    s.push(ans);
  }
  while (!s.empty()) {
    printf("%I64d ", s.top());
    s.pop();
  }
}
