#include <bits/stdc++.h>
using namespace std;
const int MX = 509;
const long long mod = 1e9 + 7;
const long long inf = (1ll << 61);
long long n, dis[MX][MX], a[MX], ex[MX], ans;
vector<long long> sol;
int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%lld", &dis[i][j]);
    }
  }
  for (int i = n; i >= 1; i--) scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    int node = a[i];
    ex[node] = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dis[i][j] = min(dis[i][j], dis[i][node] + dis[node][j]);
      }
    }
    ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ex[i] && ex[j]) ans += dis[i][j];
      }
    }
    sol.push_back(ans);
  }
  reverse(sol.begin(), sol.end());
  for (auto pp : sol) printf("%lld ", pp);
}
