#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
char s[16500];
int a[2][65540];
int pre[65540], n, m, cnt = 0;
int find(int x) {
  if (x != pre[x]) pre[x] = find(pre[x]);
  return pre[x];
}
int merge(int x, int y) {
  int prex = find(x);
  int prey = find(y);
  if (prex == prey) return 0;
  pre[prex] = prey;
  cnt--;
  return 1;
}
int pos(int now, int i) { return now * m + i; }
int main() {
  scanf("%d%d", &n, &m);
  cnt = 0;
  for (int i = 1; i <= n; i++) {
    int now = i & 1;
    scanf("%s", s + 1);
    for (int j = 1; j <= m / 4; j++) {
      char c = s[j];
      int x;
      if (isdigit(c))
        x = c - '0';
      else
        x = c - 'A' + 10;
      for (int k = 0; k < 4; k++) {
        int temp = (x >> k) & 1;
        a[now][(j - 1) * 4 + 4 - k] = temp;
        cnt += temp;
      }
    }
    for (int j = 1; j <= m; j++) pre[pos(now, j)] = pos(now, j);
    for (int j = 1; j < m; j++) {
      int p1 = pos(now, j), p2 = pos(now, j + 1);
      if (a[now][j] && a[now][j + 1]) merge(p1, p2);
    }
    for (int j = 1; j <= m; j++) {
      int p1 = pos(now ^ 1, j), p2 = pos(now, j);
      if (a[now ^ 1][j] && a[now][j]) merge(p1, p2);
    }
  }
  cout << cnt << endl;
  return 0;
}
