#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const long long inf = 1e18;
long long g[505][505], d[505][505], ans[505];
int arr[505];
int n;
int main() {
  cin >> n;
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (n); j++) cin >> g[i][j];
  for (int i = 0; i < (n); i++) {
    cin >> arr[i];
    arr[i]--;
  }
  reverse(arr, arr + n);
  for (int i = 0; i < (n); i++)
    for (int j = 0; j < (n); j++) d[i][j] = inf;
  for (int i = 0; i < (n); i++) d[i][i] = 0;
  for (int k = 0; k < (n); k++) {
    for (int i = 0; i < (k + 1); i++) {
      d[arr[k]][arr[i]] = g[arr[k]][arr[i]];
      d[arr[i]][arr[k]] = g[arr[i]][arr[k]];
    }
    for (int i = 0; i < (k + 1); i++)
      for (int j = 0; j < (k + 1); j++) {
        int u = arr[i];
        int v = arr[j];
        int x = arr[k];
        if (d[u][v] + d[v][x] < d[u][x]) d[u][x] = d[u][v] + d[v][x];
        if (d[x][u] + d[u][v] < d[x][v]) d[x][v] = d[x][u] + d[u][v];
      }
    for (int i = 0; i < (k + 1); i++)
      for (int j = 0; j < (k + 1); j++) {
        int u = arr[i];
        int v = arr[j];
        int x = arr[k];
        d[u][v] = min(d[u][v], d[u][x] + d[x][v]);
        if (d[u][v] < inf) ans[n - 1 - k] += d[u][v];
      }
  }
  for (int i = 0; i < (n); i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
