#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t = 1;
  while (t--) {
    long long n;
    cin >> n;
    vector<vector<long long>> v(n, vector<long long>(n));
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cin >> v[i][j];
      }
    }
    vector<long long> inp(n);
    for (long long i = 0; i < n; i++) {
      cin >> inp[i];
    }
    reverse((inp).begin(), (inp).end());
    vector<long long> ans(n);
    vector<long long> vis(n);
    for (long long k = 0; k < n; k++) {
      long long num = inp[k];
      vis[num - 1] = 1;
      for (long long i = 0; i < n; i++) {
        for (long long j = 0; j < n; j++) {
          v[i][j] = min(v[i][j], v[i][num - 1] + v[num - 1][j]);
          if (vis[i] == 1 && vis[j] == 1) ans[k] += v[i][j];
        }
      }
    }
    reverse((ans).begin(), (ans).end());
    for (auto i : ans) {
      cout << i << " ";
    }
    cout << "\n";
  }
}
