#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 15;
int m, ans, par[N], bef;
short newval[N / 2], n;
bitset<N / 2> st;
short val(char c) {
  if (isalpha(c)) return c - 'A' + 10;
  return c - '0';
}
int get(int u) { return par[u] == u ? u : par[u] = get(par[u]); }
void connect(int a, int b) {
  a = get(a), b = get(b);
  if (b > a) swap(a, b);
  if (a != b) ans--;
  par[a] = b;
}
int main() {
  scanf("%d%d", &n, &m);
  int last = m;
  for (int i = 0; i < N; i++) par[i] = i;
  m /= 4;
  char c[N / 4] = {};
  for (int i = 0; i < n; i++) {
    int cpar[N / 2];
    memset(newval, -1, sizeof newval);
    scanf("%s", c);
    bef = 0;
    for (int j = 0; j < m; j++) {
      short x = val(c[j]);
      for (int k = 3; k + 1; k--) {
        bool y = !!(x & (1 << k));
        if (!y) {
          cpar[bef] = bef;
          st[bef] = 0;
          bef++;
          continue;
        }
        ans++;
        if (y && st[bef]) connect(bef + last, bef);
        st[bef] = 1;
        int lol = par[bef + last];
        if (lol >= last) lol -= last;
        if (newval[lol] == -1) newval[lol] = bef;
        cpar[bef] = newval[lol];
        bef++;
      }
    }
    for (int j = 0; j < last; j++) par[j] = cpar[j];
    for (int j = 0; j + 1 < last; j++)
      if (st[j] && st[j + 1]) connect(j, j + 1);
    for (int j = last; j < last + last; j++) par[j] = j;
  }
  printf("%d\n", ans);
}
