#include <bits/stdc++.h>
#define BIT(x , i) ((x >> i) & 1)
using namespace std;
int const N = 12 ;
int S ;
int a[N][N] ;
int T[N][N][N][N][N] ;
long long dp[N][N][N][N][N][N] ;
long long CALC(int id, int h1, int h2, int h3, int h4, int h5, int lim)
{
if(id > 10) return 1 ;
long long res = dp[id][h1][h2][h3][h4][h5] ;
if(res != -1) return res ;
res = 0 ;
for(int tmp = 0 ; tmp <= 31 ; tmp ++)
{
int t1, t2, t3, t4, t5 ;
bool ok = true ;
for(int i = 0 ; i < 5 ; i ++) if(BIT(tmp, i) == 0 && a[id][i + 1] == 1) ok = false ;
if(ok == false) continue ;
t1 = h1 + 1 ;
t2 = h2 + 1 ;
t3 = h3 + 1 ;
t4 = h4 + 1 ;
t5 = h5 + 1 ;
if(BIT(tmp, 0) == 1) t1 = 0 ;
if(BIT(tmp, 1) == 1) t2 = 0 ;
if(BIT(tmp, 2) == 1) t3 = 0 ;
if(BIT(tmp, 3) == 1) t4 = 0 ;
if(BIT(tmp, 4) == 1) t5 = 0 ;
if(T[t1][t2][t3][t4][t5] <= lim) res += CALC(id + 1, t1, t2, t3, t4, t5, lim) ;
}
dp[id][h1][h2][h3][h4][h5] = res ;
return res ;
}
int MAXS(int h1, int h2, int h3, int h4, int h5)
{
int h[N], L[N], R[N] ;
h[1] = h1, h[2] = h2, h[3] = h3, h[4] = h4, h[5] = h5 ;
h[0] = h[6] = -1 ;
L[1] = 1 ;
for(int i = 2 ; i <= 5 ; i ++)
{
L[i] = i ;
while(h[L[i] - 1] >= h[i]) L[i] = L[L[i] - 1] ;
}
R[5] = 5 ;
for(int i = 4 ; i >= 1 ; i --)
{
R[i] = i ;
while(h[R[i] + 1] >= h[i]) R[i] = R[R[i] + 1] ;
}
int tmp = 0 ;
for(int i = 1 ; i <= 5 ; i ++) tmp = max(tmp, (R[i] - L[i] + 1) * h[i]) ;
return tmp ;
}
void INIT()
{
for(int i = 0 ; i <= 10 ; i ++)
for(int j = 0 ; j <= 10 ; j ++)
for(int k = 0 ; k <= 10 ; k ++)
for(int p = 0 ; p <= 10 ; p ++)
for(int q = 0 ; q <= 10 ; q ++) T[i][j][k][p][q] = MAXS(i, j, k, p, q) ;
}
int main()
{
ios_base::sync_with_stdio(NULL) ;
cin.tie(0) ;
cout.tie(0) ;
cin >> S ;
for(int i = 1 ; i <= 5 ; i ++)
for(int j = 1 ; j <= 10 ; j ++)
{
char c ;
cin >> c ;
a[j][i] = c - '0' ;
}
INIT() ;
memset(dp, -1, sizeof(dp)) ;
long long ans1 = CALC(1, 0, 0, 0, 0, 0, S) ;
memset(dp, - 1, sizeof(dp)) ;
long long ans2 = CALC(1, 0, 0, 0, 0, 0, S - 1) ;
cout << ans1 - ans2 ;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgQklUKHggLCBpKSAoKHggPj4gaSkgJiAxKQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGNvbnN0IE4gPSAxMiA7CmludCBTIDsKaW50IGFbTl1bTl0gOwppbnQgVFtOXVtOXVtOXVtOXVtOXSA7CmxvbmcgbG9uZyBkcFtOXVtOXVtOXVtOXVtOXVtOXSA7CmxvbmcgbG9uZyBDQUxDKGludCBpZCwgaW50IGgxLCBpbnQgaDIsIGludCBoMywgaW50IGg0LCBpbnQgaDUsIGludCBsaW0pCnsKICAgIGlmKGlkID4gMTApIHJldHVybiAxIDsKICAgIGxvbmcgbG9uZyByZXMgPSBkcFtpZF1baDFdW2gyXVtoM11baDRdW2g1XSA7CiAgICBpZihyZXMgIT0gLTEpIHJldHVybiByZXMgOwogICAgcmVzID0gMCA7CiAgICBmb3IoaW50IHRtcCA9IDAgOyB0bXAgPD0gMzEgOyB0bXAgKyspCiAgICB7CiAgICAgICAgaW50IHQxLCB0MiwgdDMsIHQ0LCB0NSA7CiAgICAgICAgYm9vbCBvayA9IHRydWUgOwogICAgICAgIGZvcihpbnQgaSA9IDAgOyBpIDwgNSA7IGkgKyspIGlmKEJJ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ID0gNSA7CiAgICBmb3IoaW50IGkgPSA0IDsgaSA+PSAxIDsgaSAtLSkKICAgIHsKICAgICAgICBSW2ldID0gaSA7CiAgICAgICAgd2hpbGUoaFtSW2ldICsgMV0gPj0gaFtpXSkgUltpXSA9IFJbUltpXSArIDFdIDsKICAgIH0KICAgIGludCB0bXAgPSAwIDsKICAgIGZvcihpbnQgaSA9IDEgOyBpIDw9IDUgOyBpICsrKSB0bXAgPSBtYXgodG1wLCAoUltpXSAtIExbaV0gKyAxKSAqIGhbaV0pIDsKICAgIHJldHVybiB0bXAgOwp9CnZvaWQgSU5JVCgpCnsKICAgIGZvcihpbnQgaSA9IDAgOyBpIDw9IDEwIDsgaSArKykKICAgICAgICBmb3IoaW50IGogPSAwIDsgaiA8PSAxMCA7IGogKyspCiAgICAgICAgICAgIGZvcihpbnQgayA9IDAgOyBrIDw9IDEwIDsgayArKykKICAgICAgICAgICAgICAgIGZvcihpbnQgcCA9IDAgOyBwIDw9IDEwIDsgcCArKykKICAgICAgICAgICAgICAgICAgICBmb3IoaW50IHEgPSAwIDsgcSA8PSAxMCA7IHEgKyspIFRbaV1bal1ba11bcF1bcV0gPSBNQVhTKGksIGosIGssIHAsIHEpIDsKfQppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oTlVMTCkgOwogICAgY2luLnRpZSgwKSA7CiAgICBjb3V0LnRpZSgwKSA7CiAgICBjaW4gPj4gUyA7CiAgICBmb3IoaW50IGkgPSAxIDsgaSA8PSA1IDsgaSArKykKICAgICAgICBmb3IoaW50IGogPSAxIDsgaiA8PSAxMCA7IGogKyspCiAgICAgICAgewogICAgICAgICAgICBjaGFyIGMgOwogICAgICAgICAgICBjaW4gPj4gYyA7CiAgICAgICAgICAgIGFbal1baV0gPSBjIC0gJzAnIDsKICAgICAgICB9CiAgICBJTklUKCkgOwogICAgbWVtc2V0KGRwLCAtMSwgc2l6ZW9mKGRwKSkgOwogICAgbG9uZyBsb25nIGFuczEgPSBDQUxDKDEsIDAsIDAsIDAsIDAsIDAsIFMpIDsKICAgIG1lbXNldChkcCwgLSAxLCBzaXplb2YoZHApKSA7CiAgICBsb25nIGxvbmcgYW5zMiA9IENBTEMoMSwgMCwgMCwgMCwgMCwgMCwgUyAtIDEpIDsKICAgIGNvdXQgPDwgYW5zMSAtIGFuczIgOwogICAgcmV0dXJuIDA7Cn0=