#include <bits/stdc++.h>
using namespace std;
const int maxn = (1 << 15) + 100;
int n, m;
string cur, prv;
int d[maxn], c[maxn], head[maxn];
int ans;
string num(char c) {
  string ret = "";
  int x;
  x = c >= 'A' ? c - 'A' + 10 : c - '0';
  for (int i = 8; i; i /= 2)
    if (x & i)
      ret += '1';
    else
      ret += '0';
  return ret;
}
string cng(string s) {
  string ret = "";
  for (int i = 0; i < s.size(); i++) {
    ret += num(s[i]);
  }
  return ret;
}
int get(int v) { return d[v] == v ? v : d[v] = get(d[v]); }
int mrg(int v, int u) {
  v = get(v), u = get(u);
  if (v == u) return 0;
  return d[u] = v, 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = m; i--; prv += '0')
    ;
  for (int i = m * 2; i--; d[i] = i)
    ;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    cur = cng(s);
    {
      int x = i & 1;
      int y = 1 - x;
      for (int j = 0; j < m; j++) {
        if (cur[j] - '0') {
          ans++;
          if (prv[j] - '0') ans -= mrg(2 * j + x, 2 * j + y);
          if (j and cur[j - 1] - '0') ans -= mrg(2 * j + x, 2 * j + x - 2);
        }
      }
      for (int j = 0; j < m; j++) d[2 * j + y] = 2 * j + y;
    }
    prv = cur;
  }
  cout << ans << endl;
  return 0;
}
