#include <bits/stdc++.h>
using namespace std;
int n, a[1000][1000], b[10000], cnt;
long long ans[10000];
bool vis[10000];
int main() {
  std::ios::sync_with_stdio(false);
  ;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; i++) cin >> b[i];
  cnt = 0;
  for (int i = n; i >= 1; i--) {
    int now = b[i];
    vis[now] = true;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++)
        a[j][k] = min(a[j][k], a[j][now] + a[now][k]);
    long long sum = 0;
    for (int j = 1; j <= n; j++)
      for (int k = 1; k <= n; k++) {
        if (vis[j] == false || vis[k] == false) continue;
        sum += a[j][k];
      }
    ans[++cnt] = sum;
  }
  for (int i = cnt; i > 0; i--) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
