#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9, PI = acos(-1);
const long long N = 501, oo = INT_MAX, M = 2, mod = 2012;
long long dis[N][N], vis[N], arr[N], ans[N];
long long n;
void floyd(long long node) {
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      dis[i][j] = min(dis[i][j], dis[i][node] + dis[node][j]);
      if (vis[i] && vis[j]) ans[node] += dis[i][j];
    }
  }
}
int32_t main() {
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> dis[i][j];
  for (long long i = 1; i <= n; i++) cin >> arr[i];
  for (long long i = n; i > 0; --i) {
    long long node = arr[i];
    vis[node]++;
    floyd(node);
  }
  for (long long i = 1; i <= n; i++) cout << ans[arr[i]] << " ";
  return 0;
}
