#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int n, m;
int a[(1 << 14) + 5][2];
int fa[(1 << 15) + 5];
int ans;
inline int fifa(int x) { return fa[x] == x ? x : fa[x] = fifa(fa[x]); }
inline void join(int x, int y) {
  int px = fifa(x), py = fifa(y);
  if (px == py) return;
  fa[px] = py;
  ans--;
}
inline int get_int(char ch) {
  if (ch >= '0' && ch <= '9')
    return ch - '0';
  else
    return ch - 'A' + 10;
}
int main() {
  scanf("%d%d", &n, &m);
  for (register int i = 1; i <= n; i++) {
    getchar();
    int cur = (i & 1);
    for (register int j = 1; j <= m / 4; j++) {
      char ch = getchar();
      int x = get_int(ch);
      ans += (a[(j - 1) * 4 + 1][cur] = (x >> 3) & 1);
      ans += (a[(j - 1) * 4 + 2][cur] = (x >> 2) & 1);
      ans += (a[(j - 1) * 4 + 3][cur] = (x >> 1) & 1);
      ans += (a[(j - 1) * 4 + 4][cur] = (x >> 0) & 1);
    }
    int delta = cur * m;
    for (register int j = 1; j <= m; j++) fa[delta + j] = delta + j;
    for (register int j = 1; j <= m - 1; j++)
      if (a[j][cur] && a[j + 1][cur]) join(delta + j, delta + j + 1);
    for (register int j = 1; j <= m; j++)
      if (a[j][cur ^ 1] && a[j][cur]) join((cur ^ 1) * m + j, delta + j);
  }
  printf("%d\n", ans);
  return 0;
}
