#include <bits/stdc++.h>
using namespace std;
const int mx = 1e2 + 10;
const long long mod = 1e9 + 31;
mt19937 mtrand(time(0));
int n, m;
char s[2][1 << 12];
int mp[2][1 << 14];
int f[1 << 15], ans, pr[1 << 15];
int unfd(int x) {
  int r = x;
  while (r != f[r]) r = f[r];
  int t;
  while (f[x] != r) t = f[x], f[x] = r, x = t;
  return r;
}
void unjoin(int x, int y) {
  int fx = unfd(x), fy = unfd(y);
  if (fx != fy) {
    --ans;
    f[fx] = fy;
  }
}
int main() {
  int num;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    int id = i & 1, nid = id ^ 1;
    scanf("%s", s[id]);
    num = 0;
    for (int j = 0; j < m / 4; ++j) {
      if (s[id][j] >= '0' && s[id][j] <= '9')
        num = s[id][j] - '0';
      else
        num = s[id][j] - 'A' + 10;
      for (int k = 0; k < 4; ++k) {
        mp[id][j * 4 + k] = (num >> (3 - k)) & 1;
        if (mp[id][j * 4 + k] == 1) ++ans;
      }
    }
    for (int j = m; j < m * 2; ++j) f[j] = j;
    if (i > 0) {
      for (int j = 0; j < m; ++j) {
        if (mp[nid][j] & mp[id][j]) unjoin(j, m + j);
      }
    }
    for (int j = 1; j < m; ++j) {
      if (mp[id][j - 1] & mp[id][j]) unjoin(m + j - 1, m + j);
    }
    for (int j = m; j < m * 2; ++j) {
      pr[unfd(j)] = j - m;
    }
    for (int j = 0; j < m; ++j) {
      f[j] = pr[unfd(j + m)];
    }
  }
  cout << ans << endl;
}
