#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;
int n, m;
int parent[512];
bool seen[512];
int capacity[512][512];
int SRC = 0;
int SNK;

bool bfs() {
  memset(parent, -1, sizeof(parent));
  memset(seen, 0, sizeof(seen));

  queue<int> q;
  q.push(SRC);
  seen[SRC] = true;
  while (!q.empty()) {
    int cur = q.front();
    if (cur == SNK) return true;
    q.pop();

    for (int i = SRC; i <= SNK; ++i) {
      if (!seen[i] && capacity[cur][i]) {
	seen[i] = true;
	parent[i] = cur;
	q.push(i);
      }
    }
  }
  return false;
}

int trace_path() {
  int cur = SNK;
  int prev = parent[SNK];
  int flow = capacity[prev][cur];
  while (prev != -1) {
    flow = min(flow, capacity[prev][cur]);
    cur = prev;
    prev = parent[cur];
  }
  return flow;
}

void adjust_capacity(int f) {
  int cur = SNK;
  int prev = parent[SNK];
  while (prev != -1) {
    capacity[cur][prev] += f;
    capacity[prev][cur] -= f;
    cur = prev;
    prev = parent[cur];
  }
}

int maxflow() {
  int f = 0;
  while (bfs()) {
    int temp = trace_path();
    adjust_capacity(temp);
    f += temp;
  }
  return f;
}

int main() {
  while (cin >> n >> m) {
    if (!n && !m) return 0;
    SNK = n+1;
    int tx, ty;
    memset(capacity, 0, sizeof(capacity));
    for (int i = 1; i <= n; ++i) {
      cin >> tx;
      if (tx == 1) capacity[SRC][i] = 1;
      else capacity[i][SNK] = 1;
    }

    for (int i = 0; i < m; ++i) {
      cin >> tx >> ty;
      capacity[tx][ty] = capacity[ty][tx] = 1;
    }

    cout << maxflow() << endl;
  }
}
