#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int64_t n, dist[N][N], q[N], loc[N], ans[N];
int main() {
  cin >> n;
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < n + 1; j++) cin >> dist[i][j];
  for (int i = 1; i < n + 1; i++) {
    cin >> q[n + 1 - i];
    loc[q[n + 1 - i]] = n + 1 - i;
  }
  for (int m = 1; m < n + 1; m++)
    for (int i = 1; i < n + 1; i++)
      for (int j = 1; j < n + 1; j++) {
        dist[i][j] = min(dist[i][j], dist[i][q[m]] + dist[q[m]][j]);
        if (loc[i] <= m && loc[j] <= m) ans[m] += dist[i][j];
      }
  for (int i = 1; i < n + 1; i++) cout << ans[n + 1 - i] << " \n"[i == n];
}
