#include <cstdio>
#include <cstring>
#include <numeric>
#include <emmintrin.h>

#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long llong;

char A[101][128], B[101][128];
__m128i mado[101][16], pic[101][16];
int a[4];
int n, m;
int main()
{
	while (scanf("%d%d", &m, &n), (n|m)) {
		memset(A, 0, sizeof(A));
		memset(B, 0, sizeof(B));
		int t;
		rep(i, n) rep(j, n) { scanf("%d", &t); A[i][j] = (t+1) - '0'; }
		rep(i, m) rep(j, m) { scanf("%d", &t); B[i][j] = (t+1) - '0'; }
		memcpy(mado, A, sizeof(A));
		memcpy(pic, B, sizeof(B));

		rep(i, n) {
			rep(j, n-m+1) {
				rep(k, m) rep(l, m>>4) {
					__m128i t = _mm_or_si128(mado[k][l], pic[i+k][l]);
					t = _mm_xor_si128(t, pic[k][l]);
					_mm_store_si128((__m128i*)a, t);
					if (std::accumulate(a, a+4, 0) != 0) goto BREAK;
				}
BREAK:
		//		rep(k, m) REP(l, 1, m>>4)
		//			mado[i+k][l] = _mm_alignr_epi8(mado[i+k][l], mado[i+k][l-1], 1);
			}
			memcpy(mado, A, sizeof(A));
		}
	}
	return 0;
}