#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 1, mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int dist[n][n], link[n];
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) cin >> dist[i][j];
  for (long long i = 0; i < n; i++) cin >> link[i], link[i]--;
  vector<bool> vis(n, false);
  vector<long long> ans(n, 0);
  for (long long i = n - 1; i >= 0; i--) {
    vis[link[i]] = true;
    for (long long j = 0; j < n; j++)
      for (long long k = 0; k < n; k++) {
        if (dist[j][k] > dist[j][link[i]] + dist[link[i]][k])
          dist[j][k] = dist[j][link[i]] + dist[link[i]][k];
        if (vis[j] && vis[k]) ans[i] += dist[j][k];
      }
  }
  for (long long i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  return 0;
}
