#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<vector<long long>> adj(n + 1, vector<long long>(n + 1, 0));
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      cin >> adj[i][j];
    }
  }
  vector<long long> del(n + 1), v, ans;
  for (long long i = 1; i <= n; i++) cin >> del[i];
  for (long long k = n; k >= 1; k--) {
    v.push_back(del[k]);
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][del[k]] + adj[del[k]][j]);
      }
    }
    long long sum = 0;
    for (long long i = 0; i < v.size(); i++) {
      for (long long j = 0; j < v.size(); j++) {
        sum += adj[v[i]][v[j]];
      }
    }
    ans.push_back(sum);
  }
  for (long long i = ans.size() - 1; i >= 0; i--) {
    cout << ans[i] << ' ';
  }
  cout << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long qu = 1;
  for (long long cas = 1; cas <= qu; cas++) {
    solve();
  }
}
