#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
const long long N = 5e2 + 5;
long long n, a[N][N], b[N][N], d[N][N], vis[N], x, sum;
vector<long long> v, ans;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n, v.resize(n);
  for (long long i = 1; i <= n; i++)
    for (long long j = 1; j <= n; j++) cin >> b[i][j], d[i][j] = 1e9;
  for (long long i = 0; i < n; i++) cin >> v[i];
  reverse(v.begin(), v.end());
  for (long long i = 0; i < n; i++) {
    x = v[i], vis[x] = 1, sum = 0;
    for (long long j = 1; j <= n; j++)
      if (vis[j])
        a[j][x] = b[j][x], a[x][j] = b[x][j], d[j][x] = min(d[j][x], a[j][x]),
        d[x][j] = min(d[x][j], a[x][j]);
    for (long long j = 1; j <= n; j++)
      if (vis[j])
        for (long long k = 1; k <= n; k++)
          if (vis[k])
            d[x][j] = min(d[x][j], d[x][k] + d[k][j]),
            d[j][x] = min(d[j][x], d[j][k] + d[k][x]);
    for (long long j = 1; j <= n; j++)
      if (vis[j])
        for (long long k = 1; k <= n; k++)
          if (vis[k]) d[j][k] = min(d[j][k], d[j][x] + d[x][k]);
    for (long long j = 1; j <= n; j++)
      if (vis[j])
        for (long long k = 1; k <= n; k++)
          if (vis[k]) sum += d[j][k];
    ans.push_back(sum);
  }
  reverse(ans.begin(), ans.end());
  for (auto &i : ans) cout << i << " ";
  return 0;
}
