#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int e = 5100 * 4;
int cp[2][e];
int n, m;
int fa[e * 2];
long long ans = 0;
int father(int x) {
  if (fa[x] == x) return x;
  fa[x] = father(fa[x]);
  return fa[x];
}
inline void hb(int u, int v) {
  long long fu = father(u), fv = father(v);
  if (fu == fv) return;
  fa[fv] = fu;
  ans--;
}
int main() {
  cin >> n >> m;
  ans = n * m;
  for (int t = 1; t <= n; t++) {
    int now = t % 2, las = (t + 1) % 2;
    for (int j = 1; j <= m / 4; j++) {
      char q;
      q = getchar();
      while (!((q <= '9' and q >= '0') or (q <= 'F' and q >= 'A')))
        q = getchar();
      int w = 0;
      if (q <= '9' and q >= '0')
        w = q - '0';
      else
        w = q - 'A' + 10;
      cp[now][j * 4] = (w & 1);
      w = w >> 1;
      cp[now][j * 4 - 1] = (w & 1);
      w = w >> 1;
      cp[now][j * 4 - 2] = (w & 1);
      w = w >> 1;
      cp[now][j * 4 - 3] = (w & 1);
      w = w >> 1;
    }
    for (int i = 1; i <= m; i++) {
      if (cp[now][i] == 0) ans--;
    }
    int lase = las * e, nowe = now * e;
    for (int i = 1; i <= m; i++) {
      fa[lase + i] = lase + i;
    }
    for (int i = 1; i <= m; i++) {
      int pl = lase + i;
      if (cp[now][i] == 0) continue;
      if (cp[las][i] == 1) hb(pl, nowe + i);
      if (cp[now][i - 1] == 1) hb(pl - 1, pl);
    }
  }
  cout << ans;
  return 0;
}
