#include <bits/stdc++.h>
using namespace std;
long long g[501][501], d[501][501];
vector<long long> v;
int n;
vector<long long> ans;
bool vis[501];
void floyd_warshall() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) d[i][j] = g[i][j];
  for (int k = n - 1; k >= 0; k--) {
    long long sum = 0;
    int c = v[k];
    vis[c] = 1;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        d[i][j] = min(d[i][j], d[i][c] + d[c][j]);
        if (i != j && vis[i] && vis[j]) sum += d[i][j];
      }
    ans.push_back(sum);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> g[i][j];
  for (int i = 0; i < n; i++) {
    int u;
    cin >> u;
    v.push_back(u - 1);
  }
  floyd_warshall();
  for (int i = n - 1; i >= 0; i--) {
    cout << ans[i];
    if (i > 0) cout << " ";
  }
}
