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