#include <bits/stdc++.h>
using namespace std;
const int N = 550;
int x[N], a[N][N];
long long ans[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = n; i >= 1; i--) {
    scanf("%d", &x[i]);
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i == j) continue;
        if (a[x[i]][x[k]] + a[x[k]][x[j]] < a[x[i]][x[j]]) {
          a[x[i]][x[j]] = (a[x[i]][x[k]] + a[x[k]][x[j]]);
        }
        if (k >= i && k >= j) ans[k] += (long long)a[x[i]][x[j]];
      }
    }
  }
  for (int i = n; i >= 1; i--) {
    printf("%lld ", ans[i]);
  }
  printf("\n");
}
