#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000,100000000")
using namespace std;
const long long inf = 1e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
long long d[505][505];
long long p[505];
long long used[505];
long long ans[505];
int main(void) {
  long long n;
  cin >> n;
  for (long long(i) = 0; (i) < (long long)(n); (i)++)
    for (long long(j) = 0; (j) < (long long)(n); (j)++) cin >> d[i][j];
  for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> p[n - i - 1];
  for (long long(k) = 0; (k) < (long long)(n); (k)++) {
    long long x = p[k] - 1;
    used[x] = 1;
    long long now = 0;
    for (long long(i) = 0; (i) < (long long)(n); (i)++)
      for (long long(j) = 0; (j) < (long long)(n); (j)++) {
        d[i][j] = min(d[i][j], d[i][x] + d[x][j]);
        if (used[i] && used[j]) now += d[i][j];
      }
    ans[n - k - 1] = now;
  }
  for (long long(i) = 0; (i) < (long long)(n); (i)++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
