#include <bits/stdc++.h>
const double Pi = acos(-1.0);
using namespace std;
int n, m;
int par[1 << 15 + 5], bit[1 << 14 + 5];
int findpar(int x) {
  if (par[x] != x) par[x] = findpar(par[x]);
  return par[x];
}
int cntm;
void merge(int x, int y) {
  int p1 = findpar(x), p2 = findpar(y);
  if (p1 == p2) return;
  if (p1 > p2) swap(p1, p2);
  par[p1] = p2;
  cntm++;
}
char s[1 << 14 + 5];
int main(int argc, char** argv) {
  scanf("%d %d", &n, &m);
  int c1 = 0, val;
  for (int i = 0; i < n; i++) {
    scanf("%s", &s);
    for (int j = 0; j < (m >> 2); j++) {
      val = (s[j] >= '0' && s[j] <= '9') ? (s[j] - '0') : (s[j] - 'A') + 10;
      for (int k = 4; k >= 1; k--) {
        bit[(j << 2) + k] = (val & 1);
        val >>= 1;
        par[(j << 2) + k + m] = (j << 2) + k + m;
      }
    }
    for (int j = 1; j <= m; j++) {
      if (bit[j]) {
        c1++;
        if (bit[j - 1]) merge(j + m - 1, j + m);
        if (par[j]) merge(j, j + m);
      }
    }
    for (int j = 1; j <= m; j++)
      par[j] = (bit[j]) ? findpar(par[j + m]) - m : 0;
  }
  printf("%d\n", c1 - cntm);
  return 0;
}
