#include<bits/stdc++.h>
using namespace std;

#define pb              push_back
#define SZ(x)           (int)x.size()
#define F               first
#define S               second
#define ALL(x)          x.begin(), x.end()
#define fast            ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i, a, b)    for(int i = a; i < b; ++i)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll INF = 1e18;
const int MOD = 998244353;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;

#define int long long
const int N = 51;
int adj[N][N], arr[N][N];
int vis[N];
int n, k;
ll f[N];
void dfs(int node, int &cnt) {
        if(vis[node]) return;
        vis[node] = 1;
        cnt++;
        FOR(nxt, 1, n+1) {
                if(adj[node][nxt]) {
                        dfs(nxt, cnt);
                }
        }
}

void solve() {
        cin >> n >> k;
        FOR(i, 1, n+1) FOR(j, 1, n+1) cin >> arr[i][j];
        f[0] = 1;
        FOR(i, 1, n+1) f[i] = (f[i-1] * i) % MOD;
        ll ans = 1;
        FOR(i, 1, n+1) FOR(j, 1, n+1) adj[i][j] = 0;
        FOR(i, 1, n+1) vis[i] = 0;
        FOR(x, 1, n+1) {
                FOR(y, x+1, n+1) {
                        bool ok = true;
                        FOR(r, 1, n+1) {
                                if(arr[r][x] + arr[r][y] > k) {
                                        ok = false;
                                        break;
                                }
                        }
                        if(ok) {
                                adj[x][y] = 1;
                                adj[y][x] = 1;
                        }
                }
        }
        
        ll cur = 1;
        FOR(i, 1, n+1) {
                if(!vis[i]) {
                        int cnt = 0;
                        dfs(i, cnt);
                        cur = (cur * f[cnt]) % MOD;
                }
        }
        ans = (ans * cur) % MOD;

        FOR(i, 1, n+1) FOR(j, 1, n+1) adj[i][j] = 0;
        FOR(i, 1, n+1) vis[i] = 0;
        FOR(x, 1, n+1) {
                FOR(y, x+1, n+1) {
                        bool ok = true;
                        FOR(c, 1, n+1) {
                                if(arr[x][c] + arr[y][c] > k) {
                                        ok = false;
                                        break;
                                }
                        }
                        if(ok) {
                                adj[x][y] = 1;
                                adj[y][x] = 1;
                        }
                }
        }
        
        cur = 1;
        FOR(i, 1, n+1) {
                if(!vis[i]) {
                        int cnt = 0;
                        dfs(i, cnt);
                        cur = (cur * f[cnt]) % MOD;
                }
        }
        ans = (ans * cur) % MOD;
        cout << ans << endl;
}

signed  main() {
        fast;
        int T = 1;
        // cin >> T;
        while(T--) {
                solve();
        }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -1;
#define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cout << #a << ": " << a << endl;
#define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl;
#define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; }
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }

//bool callback(int from, int to)
template <class Graph, class Callback>
void bfs(Graph& g, int start, Callback callback) {
    vector<bool> visited(g.size());
    queue<int> q;
    q.push(start);
    visited[start] = true;
    while (!q.empty()) {
        auto v = q.front(); q.pop();
        for (auto u : g[v]) {
            if (visited[u]) continue;
            visited[u] = true;
            if (callback(v,u)) q.push(u);
        }
    }
}

ll dp[1<<17][17];
int main() {
    fastio;
    ll ans=Inf;
    ll N,M,K;
    cin >> N >> M;
    vector<vector<int>> g(N);
    rep(i,M) {
        int a,b;
        cin >> a >> b, --a,--b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    cin >> K;
    vector<ll> c(K);
    rep(i,K) cin >> c[i], --c[i];
    ll d2[17][17]{};
    rep(i,K) rep(j,K) d2[i][j] = Inf;
    rep(i,K) {
        vector<ll> d(N, Inf);
        d[c[i]] = 0;
        bfs(g, c[i], [&](int from, int to){
            d[to] = 1 + d[from];
            return true;
        });
        rep(j,K) if (i != j) d2[i][j] = d[c[j]];
    }
    rep(i,1<<K) rep(j,K) dp[i][j] = Inf;
    rep(i,K) dp[1<<i][i] = 0;
    rep(p,1<<K) rep(i,K) {
        if (!(p>>i&1)) continue;
        rep(j,K) {
            if (p>>j&1) continue;
            updmin(dp[p|(1<<j)][j], dp[p][i] + d2[i][j]);
        }
    }
    rep(i,K) updmin(ans, dp[(1<<K)-1][i]);
    if (ans == Inf) ans = -1;
    else ans++;
    cout << ans << endl;
}
