#include <bits/stdc++.h>
using namespace std;
const int MaxN = int(2e6) + 256;
const long long INF = (long long)(1e15) + 1;
const int mod = (int)(1e9) + 7;
const double pi = 3.1415926535897932384626433832795;
long long n, m, a[555][555], k, T, b[555], d[555][555];
vector<long long> v, ans;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      d[i][j] = INF;
    }
    d[i][i] = 0;
  }
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = n; i >= 1; i--) {
    if (i == n) {
      v.push_back(b[i]);
      ans.push_back(0);
    } else {
      for (int j = 0; j < v.size(); j++) {
        for (int l = 0; l < v.size(); l++) {
          int u = v[j];
          int V = v[l];
          d[u][b[i]] = min(d[u][b[i]], d[u][V] + a[V][b[i]]);
          d[b[i]][u] = min(d[b[i]][u], d[V][u] + a[b[i]][V]);
        }
      }
      for (int j = 0; j < v.size(); j++) {
        for (int l = 0; l < v.size(); l++) {
          int u = v[j], V = v[l];
          d[u][V] = min(d[u][V], d[u][b[i]] + d[b[i]][V]);
        }
      }
      v.push_back(b[i]);
      long long Ans = 0;
      for (int j = 0; j < v.size(); j++) {
        for (int l = 0; l < v.size(); l++) {
          Ans += d[v[j]][v[l]];
        }
      }
      ans.push_back(Ans);
    }
  }
  for (int i = n - 1; i >= 0; i--) cout << ans[i] << " ";
  return 0;
}
