#include <bits/stdc++.h>
void read(int& x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
void read(long long& x) {
  x = 0;
  int flag = 1;
  char ch = ' ';
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= flag;
}
using namespace std;
const int maxn = (1 << 14) + 10;
int n, m, tmp[5], b[maxn], a[maxn];
char s[maxn];
bool vis[maxn];
void deal(char ch) {
  int x, pos = 4;
  memset(tmp, 0, sizeof(tmp));
  if (ch >= '0' && ch <= '9')
    x = ch - '0';
  else
    x = ch - 'A' + 10;
  for (int i = 0; i <= (int)3; i++)
    if ((1 << i) & x) tmp[4 - i] = 1;
}
int f[2][maxn], flag[maxn];
int find(int* f, int x) { return f[x] == x ? f[x] : f[x] = find(f, f[x]); }
void merge(int* f, int a, int b) {
  int r1 = find(f, a), r2 = find(f, b);
  if (r1 != r2) f[r1] = r2;
}
int main() {
  int ans = 0;
  read(n), read(m);
  n++;
  for (int i = 1; i <= (int)m; i++) f[0][i] = i;
  while (n--) {
    memset(b, 0, sizeof(b));
    memset(vis, 0, sizeof(vis));
    memset(flag, 0, sizeof(flag));
    if (n) {
      scanf("%s", s + 1);
      for (int i = 1; i <= (int)m / 4; i++) {
        deal(s[i]);
        for (int j = i * 4 - 3; j <= (int)i * 4; j++)
          b[j] = tmp[(j - 1) % 4 + 1];
      }
    }
    for (int i = 1; i <= (int)m; i++)
      if (a[i] && b[i]) vis[find(f[0], i)] = 1;
    for (int i = 1; i <= (int)m; i++)
      if (a[i] && !vis[find(f[0], i)]) {
        ans++;
        vis[find(f[0], i)] = 1;
      }
    for (int i = 1; i <= (int)m; i++) f[1][i] = i;
    for (int i = 1; i <= (int)m; i++)
      if (a[i] && b[i]) {
        int tmp = find(f[0], i);
        if (!flag[tmp])
          flag[tmp] = i;
        else
          merge(f[1], flag[tmp], i), flag[tmp] = i;
      }
    for (int i = 1; i <= (int)m - 1; i++)
      if (b[i] && b[i + 1]) merge(f[1], i, i + 1);
    memcpy(a, b, sizeof(a));
    memcpy(f[0], f[1], sizeof(f[0]));
  }
  printf("%d\n", ans);
  return 0;
}
