#include <bits/stdc++.h>
using namespace std;
const long long N = 505;
long long n;
long long a[N], ans[N], vis[N];
long long dist[N][N];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> dist[i][j];
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long idx = n; idx >= 1; idx--) {
    long long k = a[idx];
    vis[k] = 1;
    for (long long i = 1; i <= n; i++) {
      for (long long j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        if (vis[i] && vis[j]) ans[idx] += dist[i][j];
      }
    }
  }
  for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
