#include <bits/stdc++.h>
using namespace std;
const int mxN = 501;
vector<vector<long long> > adj(mxN, vector<long long>(mxN, 0));
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x;
      adj[i + 1][j + 1] = x;
    }
  }
  int v[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> v[i + 1];
  }
  vector<long long> ans(n + 1, 0);
  vector<bool> vis(n + 1, false);
  for (int k = n; k > 0; k--) {
    int nv = v[k];
    vis[nv] = true;
    for (int i = 1; i <= n; i++) {
      int j;
      for (j = 1; j <= n; j++) {
        adj[i][j] = min(adj[i][j], adj[i][nv] + adj[nv][j]);
        if (vis[i] && vis[j]) ans[k] += adj[i][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
