#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 1;
const int N = 555;
const int base = 722;
const int NMOD = 2;
const int MOD[] = {(int)1e9 + 5277, (int)1e9 + 8277, (int)1e9 + 9277};
int n;
long long a[N][N], x[N], res[N];
void Floyd() {
  for (int k = n; k > 0; k--) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        a[i][j] = min(a[i][j], a[i][x[k]] + a[x[k]][j]);
      }
    }
    for (int i = n; i >= k; i--) {
      for (int j = n; j >= k; j--) {
        res[k] += (a[x[i]][x[j]] == INF ? 0 : a[x[i]][x[j]]);
      }
    }
  }
  for (int i = 1; i <= n; i++) cout << res[i] << ' ';
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  Floyd();
  return 0;
}
