#include <bits/stdc++.h>
using namespace std;
int n, m, C;
vector<bool> M[2];
vector<int> T[2][2];
int H(char c) { return c <= '9' ? c - '0' : c - 'A' + 10; }
void find(int c, int j, int &c0, int &j0) {
  if (T[0][c][j] == c && T[1][c][j] == j) {
    c0 = c;
    j0 = j;
  } else {
    find(T[0][c][j], T[1][c][j], c0, j0);
    T[0][c][j] = c0;
    T[1][c][j] = j0;
  }
}
void uni(int c1, int j1, int c2, int j2) {
  int c10, j10, c20, j20;
  find(c1, j1, c10, j10);
  find(c2, j2, c20, j20);
  if (!(c10 == c20 && j10 == j20)) {
    T[0][c20][j20] = c10;
    T[1][c20][j20] = j10;
    --C;
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < 2; ++i) {
    M[i].resize(m);
    T[0][i].resize(m);
    T[1][i].resize(m);
  }
  int c = 0;
  char s[m / 4];
  for (int i = 0; i < n; ++i) {
    scanf(" %s", s);
    for (int j = 0; j < m / 4; ++j) {
      int v = H(s[j]);
      for (int k = 0; k < 4; ++k) M[c][4 * j + 3 - k] = (v >> k) & 1;
    }
    for (int j = 0; j < m; ++j)
      if (M[c][j]) {
        if (j > 0 && M[c][j - 1]) {
          T[0][c][j] = T[0][c][j - 1];
          T[1][c][j] = T[1][c][j - 1];
        } else {
          ++C;
          T[0][c][j] = c;
          T[1][c][j] = j;
        }
      }
    if (i > 0)
      for (int j = 0; j < m; ++j)
        if (M[c][j] && M[c ^ 1][j]) uni(c, j, c ^ 1, j);
    c ^= 1;
  }
  printf("%d\n", C);
  return 0;
}
