#include <bits/stdc++.h>
using namespace std;
const int N = 501, M = 2100050, INF = 0x3f3f3f3f;
long long ans[N];
int main() {
  int n, m, a[N][N], b[N], vis[N];
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
  }
  for (int k = n; k > 0; k--) {
    int x = b[k];
    vis[x] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (i != j) {
          a[i][j] = min(a[i][j], a[i][x] + a[x][j]);
          if (vis[i] && vis[j]) ans[k] += a[i][j];
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%I64d ", ans[i]);
  }
}
