#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:200000000")
const double EPS = 1E-9;
const int INF = 1000000000;
const long long INF64 = (long long)1E18;
const double PI = 3.1415926535897932384626433832795;
const int NMAX = 510;
int v[NMAX], x[NMAX], a[NMAX][NMAX];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < (int)(n); i++)
    for (int j = 0; j < (int)(n); j++) scanf("%d", &a[i][j]);
  for (int i = 0; i < (int)(n); i++) {
    scanf("%d", &x[i]);
    x[i]--;
  }
  vector<long long> ans;
  int sz = 0;
  for (int i = (int)(n)-1; i >= 0; i--) {
    int c = x[i];
    for (int i = 0; i < (int)(sz); i++)
      for (int j = 0; j < (int)(sz); j++)
        a[v[i]][c] = min(a[v[i]][c], a[v[i]][v[j]] + a[v[j]][c]);
    for (int i = 0; i < (int)(sz); i++)
      for (int j = 0; j < (int)(sz); j++)
        a[c][v[i]] = min(a[c][v[i]], a[v[j]][v[i]] + a[c][v[j]]);
    for (int i = 0; i < (int)(sz); i++)
      for (int j = 0; j < (int)(sz); j++)
        a[v[i]][v[j]] = min(a[v[i]][v[j]], a[v[i]][c] + a[c][v[j]]);
    v[sz++] = c;
    long long sum = 0;
    for (int i = 0; i < (int)(sz); i++)
      for (int j = 0; j < (int)(sz); j++) sum += a[v[i]][v[j]];
    ans.push_back(sum);
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < (int)(n); i++) printf("%I64d ", ans[i]);
  return 0;
}
