#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int d[n][n];
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < n; j++) cin >> d[i][j];
  }
  long long int a[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<long long int> ans;
  for (long long int i = n - 1; i >= 0; i--) {
    long long int c = 0;
    for (long long int j = 0; j < n; j++) {
      for (long long int k = 0; k < n; k++) {
        d[j][k] = min(d[j][k], d[j][a[i]] + d[a[i]][k]);
      }
    }
    for (long long int j = i; j < n; j++) {
      for (long long int k = j + 1; k < n; k++) {
        c += d[a[j]][a[k]];
        c += d[a[k]][a[j]];
      }
    }
    ans.push_back(c);
  }
  reverse(ans.begin(), ans.end());
  for (long long int i = 0; i < ans.size(); i++) {
    cout << ans[i] << " ";
  }
}
