#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5, M = 205, INF = 1e9, MOD = 998244353;
long long arr[509][509];
long long mat[509][509];
int trteb[N];
void runtestcase() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) mat[i][j] = 1e18;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j) cin >> arr[i][j];
  for (int i = 0; i < n; ++i) cin >> trteb[i];
  vector<long long> vec;
  vector<int> m3aya;
  for (int k = n - 1; k >= 0; --k) {
    int node = trteb[k] - 1;
    m3aya.push_back(node);
    for (int ii = 0; ii < ((int)m3aya.size()); ++ii) {
      int i = m3aya[ii];
      mat[i][node] = min(mat[i][node], arr[i][node]);
      mat[node][i] = min(mat[node][i], arr[node][i]);
    }
    for (int ii = 0; ii < ((int)m3aya.size()); ++ii) {
      for (int jj = 0; jj < ((int)m3aya.size()); ++jj) {
        int i = m3aya[ii], j = m3aya[jj];
        mat[j][node] = min(mat[j][node], mat[j][i] + mat[i][node]);
      }
    }
    for (int ii = 0; ii < ((int)m3aya.size()); ++ii) {
      for (int jj = 0; jj < ((int)m3aya.size()); ++jj) {
        int i = m3aya[ii], j = m3aya[jj];
        mat[node][j] = min(mat[node][j], mat[node][i] + mat[i][j]);
      }
    }
    for (int ii = 0; ii < ((int)m3aya.size()); ++ii) {
      for (int jj = 0; jj < ((int)m3aya.size()); ++jj) {
        int i = m3aya[ii], j = m3aya[jj];
        mat[i][j] = min(mat[i][j], mat[i][node] + mat[node][j]);
      }
    }
    long long ans = 0;
    for (int ii = 0; ii < ((int)m3aya.size()); ++ii) {
      for (int jj = 0; jj < ((int)m3aya.size()); ++jj) {
        int i = m3aya[ii], j = m3aya[jj];
        ans += mat[i][j];
      }
    }
    vec.push_back(ans);
  }
  reverse(vec.begin(), vec.end());
  for (auto it : vec) cout << it << ' ';
}
int main() {
  cout << fixed << setprecision(3), ios::sync_with_stdio(false),
      cin.tie(nullptr), cout.tie(nullptr);
  int t = 1, tt = 1;
  while (t) {
    runtestcase();
    --t, ++tt;
  }
}
