/**
 * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
 */

#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n, k, x;
  cin >> n;
  vector<string> c(n);
  forn(i, n)
    cin >> c[i];
  vector<int> u(n, 0), is(n, 0);
  cin >> k;
  while (k--)
    cin >> x, is[x] = 1;

  int cnt, was, edges = 0;
  function<void(int)> dfs = [&]( int v ) {
    u[v] = 1, cnt++, was |= is[v];
    forn(x, n) {
      if (!u[x] && c[v][x] == '1')
        dfs(x);
      edges += (c[v][x] == '1');
    }
  };
  int black = 0, ma = 0, res = 0;
  auto F = [&]( int n ) { return n * (n - 1); };
  forn(i, n)
    if (!u[i]) {
      cnt = 0, was = 0;
      dfs(i);
      if (!was)
        black += cnt;
      else {
        res = res + F(cnt);
        ma = max(ma, cnt);
      }
    }
  res = res - F(ma) + F(black + ma);
  cout << (res - edges) / 2 << endl;
  return 0;
}
