#include <bits/stdc++.h>
using namespace std;
const long long MAX = 500 + 30;
const long long INF = 1e18;
long long n;
long long a[MAX][MAX];
long long q[MAX];
bool del[MAX];
vector<long long> ans;
long long get() {
  long long ret = 0;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++)
      if (!del[i] && !del[j]) ret += a[i][j];
  return ret;
}
int main() {
  scanf("%I64d", &n);
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) scanf("%I64d", &a[i][j]);
  for (long long i = 0; i < n; i++) {
    scanf("%I64d", &q[i]);
    q[i]--;
    del[i] = 1;
  }
  reverse(q, q + n);
  for (long long i = 0; i < n; i++) {
    long long v = q[i];
    del[v] = 0;
    for (long long j = 0; j < n; j++)
      for (long long w = 0; w < n; w++)
        a[j][w] = min(a[j][w], a[j][v] + a[v][w]);
    ans.push_back(get());
  }
  while (ans.size()) {
    printf("%I64d ", ans.back());
    ans.pop_back();
  }
  printf("\n");
  return 0;
}
