#include <bits/stdc++.h>
using namespace std;
const int maxn = 507;
long long n, adj[maxn][maxn], dist[maxn][maxn], x[maxn], ans[maxn], sum = 0;
void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> adj[i][j];
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    cin >> x[i];
    x[i]--;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      dist[i][j] = adj[x[i]][x[j]];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        dist[k][j] = min(dist[k][j], dist[k][i] + dist[i][j]);
      }
    }
    sum = 0;
    for (int j = 0; j <= i; ++j) {
      for (int k = 0; k <= i; ++k) {
        sum += dist[j][k];
      }
    }
    ans[i] = sum;
  }
  reverse(ans, ans + n);
  for (int i = 0; i < n; ++i) {
    cout << ans[i];
    if (i < n - 1) {
      cout << ' ';
    } else {
      cout << endl;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
