#include <bits/stdc++.h>
using namespace std;
long long a[501][501];
bool visited[501];
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    visited[i] = false;
    for (long long j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  vector<long long> order(n);
  for (long long i = 0; i < n; i++) {
    cin >> order[i];
    order[i]--;
  }
  visited[order[n - 1]] = true;
  vector<long long> values;
  for (long long k = 0; k < n; k++) {
    long long ans = 0;
    long long p = order[n - k - 1];
    visited[p] = true;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        a[i][j] = min(a[i][j], a[i][p] + a[p][j]);
        if (visited[i] && visited[j] && visited[p]) {
          ans += a[i][j];
        }
      }
    }
    values.push_back(ans);
  }
  for (long long i = 0; i < n; i++) {
    cout << values[n - 1 - i] << " ";
  }
}
