#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)1002;
const long long inf = (1ll << 50);
int n, m;
int d[maxn][maxn];
int x[maxn];
long long ans[maxn];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> d[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  for (int i = n; i >= 1; i--) {
    int k = x[i];
    for (int a = 1; a <= n; a++) {
      for (int b = 1; b <= n; b++) {
        d[a][b] = min(d[a][b], d[a][k] + d[k][b]);
      }
    }
    for (int a = i; a <= n; a++) {
      for (int b = i; b <= n; b++) {
        ans[i] += d[x[a]][x[b]];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
}
int main() {
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
