#include <bits/stdc++.h>
using namespace std;
const int MX = 505;
int n;
int a[MX][MX], B[MX][MX];
int x[MX], in[MX];
long long R[MX];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> a[i][j];
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    x[i]--;
  }
  int ptr = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (j != k) a[j][k] = min(a[j][k], a[j][x[i]] + a[x[i]][k]);
      }
    }
    in[ptr++] = x[i];
    for (int j = 0; j < ptr; j++) {
      for (int k = 0; k < ptr; k++) {
        if (j != k) R[i] += a[in[j]][in[k]];
      }
    }
  }
  for (int i = 0; i < n; i++) cout << R[i] << " ";
  cout << endl;
  return 0;
}
