#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:64000000")
const int N = 510;
const int INF = INT_MAX;
int n;
int a[N][N];
int x[N];
int b[N][N];
bool u[N];
long long res[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  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 >> x[i];
  }
  for (int it = n; it >= 1; it--) {
    int k = x[it];
    u[k] = true;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
      }
    }
    for (int i = 1; i <= n; i++) {
      if (!u[i]) {
        continue;
      }
      for (int j = 1; j <= n; j++) {
        if (!u[j]) {
          continue;
        }
        res[it] += a[i][j];
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << res[i] << " ";
  }
  return 0;
}
