#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
int parent[1 << 15 + 1];
int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); }
void merge(int x, int y) {
  int px = find(x), py = find(y);
  if (px == py) return;
  ans++;
  parent[px] = py;
}
int main() {
  scanf("%d %d%*c", &n, &m);
  vector<bool> last(m + 1, 0);
  for (int i = 1; i <= 2 * m; i++) parent[i] = i;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    char x;
    vector<bool> curr(m + 1, 0);
    for (int j = 1; j <= m; j += 4) {
      scanf("%c", &x);
      bool bits[4];
      int y = x <= '9' && x >= '0' ? x - '0' : x - 'A' + 10;
      curr[j] = y & 8;
      curr[j + 1] = y & 4;
      curr[j + 2] = y & 2;
      curr[j + 3] = y & 1;
    }
    for (int j = 1; j <= m; j++)
      if (curr[j]) {
        cnt++;
        if (curr[j - 1]) merge(j - 1 + m, j + m);
        if (last[j]) merge(j, j + m);
      }
    last = curr;
    if (i != n - 1) {
      scanf("%*c");
      for (int i = 1; i <= m; i++) parent[i] = parent[i + m] - m;
      for (int i = m + 1; i <= 2 * m; i++) parent[i] = i;
    }
  }
  printf("%d", cnt - ans);
  return 0;
}
