#include <bits/stdc++.h>
using namespace std;
int fa[40005], cnt, n, m;
char s[40005];
int mp[2][40005];
int Pos(int i, int j) { return i * m + j; }
int F(int x) { return x == fa[x] ? x : fa[x] = F(fa[x]); }
void Merge(int x, int y) {
  int fx = F(x), fy = F(y);
  if (fx != fy) {
    fa[fy] = fx;
    cnt--;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m * 2; i++) fa[i] = i;
  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 = 0;
      if (!isdigit(c))
        x = c - 'A' + 10;
      else
        x = c - '0';
      mp[now][(j - 1) * 4 + 1] = (x >> 3) & 1;
      cnt += (x >> 3) & 1;
      mp[now][(j - 1) * 4 + 2] = (x >> 2) & 1;
      cnt += (x >> 2) & 1;
      mp[now][(j - 1) * 4 + 3] = (x >> 1) & 1;
      cnt += (x >> 1) & 1;
      mp[now][(j - 1) * 4 + 4] = x & 1;
      cnt += x & 1;
    }
    for (int j = 1; j <= m; j++) fa[Pos(now, j)] = Pos(now, j);
    for (int j = 1; j < m; j++) {
      if (mp[now][j] && mp[now][j + 1]) Merge(Pos(now, j), Pos(now, j + 1));
    }
    for (int j = 1; j <= m; j++) {
      if (mp[now ^ 1][j] && mp[now][j]) Merge(Pos(now, j), Pos(now ^ 1, j));
    }
  }
  printf("%d", cnt);
  return 0;
}
