#include <bits/stdc++.h>
using namespace std;
int a[505][505];
int x[505];
long long d[505][505];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      cin >> a[j][i];
      d[j][i] = a[j][i];
    }
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }
  reverse(x, x + n);
  long long sum = 0;
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = min(d[i][j], d[i][x[k]] + d[x[k]][j]);
    for (int i = 0; i <= k; i++)
      for (int j = 0; j <= k; j++) sum += d[x[i]][x[j]];
    v.push_back(sum);
    sum = 0;
  }
  reverse(v.begin(), v.end());
  for (int i = 0; i < n; i++) cout << v[i] << ' ';
  cout << endl;
  return 0;
}
