#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 5;
int g[N][N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  while (T--) {
    vector<long long int> ans(N, 0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cin >> g[i][j];
      }
    }
    vector<int> order(N);
    for (int i = 1; i <= n; i++) cin >> order[i];
    vector<int> vis(N, 0);
    for (int k = n; k > 0; k--) {
      int idx = order[k];
      vis[idx] = 1;
      for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
          g[i][j] = min(g[i][j], g[i][idx] + g[idx][j]);
          if (vis[i] && vis[j]) ans[k] += g[i][j];
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      cout << ans[i] << " ";
    }
  }
  return 0;
}
