#include <bits/stdc++.h>
using namespace std;
long long a[505][505], ans[505];
int s[505];
long long _min(long long a, long long b) { return a < b ? a : b; }
int main() {
  int n, x, y, mid;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) scanf("%lld", &a[i][j]);
  for (int i = n; i >= 1; i--) scanf("%d", &s[i]);
  for (int t = 1; t <= n; t++) {
    mid = s[t];
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        x = s[i];
        y = s[j];
        a[x][y] = _min(a[x][y], a[x][mid] + a[mid][y]);
        if (i <= t && j <= t) ans[t] += a[x][y];
      }
    }
  }
  for (int i = n; i >= 1; i--) printf("%I64d ", ans[i]);
  printf("\n");
  return 0;
}
