#include <bits/stdc++.h>
using namespace std;
long long dis[505][505];
long long a[505][505], appear[505];
signed main() {
  ios::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (i == j) {
        dis[i][j] = 0;
      } else {
        dis[i][j] = INT_MAX;
      }
      dis[i][j] = a[i][j];
    }
  stack<long long> res;
  long long cur;
  long long m;
  vector<long long> turn;
  m = n;
  for (long long i = 0; i < m; i++) {
    long long x;
    cin >> x;
    turn.push_back(x);
  }
  reverse(turn.begin(), turn.end());
  vector<long long> lul;
  for (long long i = 0; i < turn.size(); i++) {
    cur = 0;
    appear[turn[i]] = 1;
    for (long long j = 1; j <= n; j++)
      for (long long k = 1; k <= n; k++)
        if (appear[j] && appear[k]) {
          dis[j][turn[i]] = min(dis[j][turn[i]], dis[j][k] + dis[k][turn[i]]);
          dis[turn[i]][j] = min(dis[turn[i]][j], dis[k][j] + dis[turn[i]][k]);
        }
    for (long long j = 1; j <= n; j++)
      for (long long k = 1; k <= n; k++)
        if (appear[j] && appear[k]) {
          dis[j][k] = min(dis[j][k], dis[j][turn[i]] + dis[turn[i]][k]);
        }
    for (long long j = 1; j <= n; j++)
      for (long long k = 1; k <= n; k++)
        if (appear[j] && appear[k]) cur += dis[j][k];
    res.push(cur);
  }
  while (!res.empty()) {
    cout << res.top() << " ";
    res.pop();
  }
}
