#include using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef vector vi; #define pb push_back #define mp make_pair #define MAXN 10000 struct Dinic { struct Edge { int to, rev; ll c, oc; ll flow() { return max(oc - c, 0LL); } // if you need flows }; vi lvl, ptr, q; vector> adj; Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {} void addEdge(int a, int b, ll c, ll rcap = 0) { adj[a].push_back({b, sz(adj[b]), c, c}); adj[b].push_back({a, sz(adj[a]) - 1, rcap, rcap}); } ll dfs(int v, int t, ll f) { if (v == t || !f) return f; for (int& i = ptr[v]; i < sz(adj[v]); i++) { Edge& e = adj[v][i]; if (lvl[e.to] == lvl[v] + 1) if (ll p = dfs(e.to, t, min(f, e.c))) { e.c -= p, adj[e.to][e.rev].c += p; return p; } } return 0; } ll calc(int s, int t) { ll flow = 0; q[0] = s; rep(L,0,31) do { // 'int L=30' maybe faster for random data lvl = ptr = vi(sz(q)); int qi = 0, qe = lvl[s] = 1; while (qi < qe && !lvl[t]) { int v = q[qi++]; for (Edge e : adj[v]) if (!lvl[e.to] && e.c >> (30 - L)) q[qe++] = e.to, lvl[e.to] = lvl[v] + 1; } while (ll p = dfs(s, t, LLONG_MAX)) flow += p; } while (lvl[t]); return flow; } bool leftOfMinCut(int a) { return lvl[a] != 0; } }; vi adj[MAXN]; int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int n, m; cin >> n >> m; rep(i, 0, m) { int u, v; cin >> u >> v; u--; v--; adj[u].pb(v); adj[v].pb(u); } string s; cin >> s; Dinic flow(2*n + 2); const int a = 2*n; const int b = a + 1; rep(i, 0, n) { int v0, v1; if (s[i] == '0') { v0 = i; v1 = n + i; flow.addEdge(a, v0, 2); flow.addEdge(v1, b, 1); flow.addEdge(v0, v1, 1e8); } else { v0 = n + i; v1 = i; flow.addEdge(a, v1, 1); flow.addEdge(v0, b, 2); flow.addEdge(v1, v0, 1e8); } for (int x : adj[i]) { int vx0, vx1; if (s[x] == '0') { vx0 = x; vx1 = n + x; // can't both be 0, can't both be 1 flow.addEdge(vx0, v0, 1e8); flow.addEdge(v1, vx1, 1e8); } else { vx0 = n + x; vx1 = x; // can't both be 0, can't both be 1 flow.addEdge(v0, vx0, 1e8); flow.addEdge(vx1, v1, 1e8); } } } int mvc = flow.calc(a, b); int mis = 3*n - mvc; int out = 2*n - mis; cout << out << endl; }