#include <bits/stdc++.h>
using namespace std;
long long n, mp[505][505], del[505], vis[505], ans[505];
signed main() {
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) scanf("%lld", &mp[i][j]);
  for (long long i = 1; i <= n; i++) scanf("%lld", &del[i]);
  for (long long tim = n; tim >= 1; tim--) {
    long long now = del[tim];
    vis[now] = 1;
    for (long long i = 1; i <= n; i++)
      for (long long j = 1; j <= n; j++) {
        mp[i][j] = min(mp[i][j], mp[i][now] + mp[now][j]);
        if (vis[i] && vis[j]) ans[tim] += mp[i][j];
      }
  }
  for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
  return 0;
}
