#include <bits/stdc++.h>
using namespace std;
long long n;
long long e[505][505];
long long dist[505][505] = {};
long long d[505];
long long a[505];
int main() {
  a[0] = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> e[i][j];
      dist[i][j] = e[i][j];
    }
  }
  for (int i = n; i >= 1; i--) {
    cin >> d[i];
  }
  for (int k = 1; k <= n; k++) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dist[d[i]][d[k]] + dist[d[k]][d[j]] < dist[d[i]][d[j]]) {
          dist[d[i]][d[j]] = dist[d[i]][d[k]] + dist[d[k]][d[j]];
        }
        if (i <= k && j <= k) ans += dist[d[i]][d[j]];
      }
    }
    a[k] = ans;
  }
  for (int i = n; i >= 1; i--) {
    cout << a[i] << ' ';
  }
}
