#include <bits/stdc++.h>
const long long INF = 1e9;
const long long N = 5000;
const double PI = 3.1415926535897932384626433832795;
using namespace std;
long long a[N][N], ar[N], n, res[N];
bool used[N];
int main() {
  ios_base ::sync_with_stdio();
  long long c, i, j;
  scanf("%lld", &n);
  for (c = 1; c <= n; c++) {
    for (i = 1; i <= n; i++) {
      scanf("%lld", &a[c][i]);
    }
  }
  for (c = 1; c <= n; c++) {
    scanf("%lld", &ar[c]);
    used[ar[c]] = 1;
  }
  for (c = n; c >= 1; c--) {
    long long as = 0;
    used[ar[c]] = 0;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        a[i][j] = min(a[i][j], a[i][ar[c]] + a[ar[c]][j]);
        if (!used[i] && !used[j]) as += a[i][j];
      }
    }
    res[c] = as;
  }
  for (c = 1; c <= n; c++) {
    printf("%lld ", res[c]);
  }
  return 0;
}
