蒟蒻的自我拯救。。。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#define ll __int64
int save[40][40],m,n;
ll dp[40][40];
bool ok(int x,int y)
{
if(x>0&&x<=n&&y<=n&&y>0)
return true;
return false;
}
ll dfs(int x,int y)
{
//printf("%d %d\n",x,y);
//system("pause");
if(dp[x][y]!=-1)
return dp[x][y];
if(x==n&&y==n)
return 1;
if(save[x][y]==0)
return 0;
ll tmp=0;
int xx,yy;
xx=x+save[x][y];
yy=y;
if(ok(xx,yy))
{
tmp+=dfs(xx,yy);
}
xx=x;
yy=y+save[x][y];
if(ok(xx,yy))
{
tmp+=dfs(xx,yy);
}
dp[x][y]=tmp;
return tmp;
}
char st[100];
int main()
{
while(scanf("%d",&n))
{
if(n==-1)
break;
if(n==0)
{
printf("0\n");
continue;
}
for(int i=1;i<=n;i++)
{
scanf("%s",st);
for(int j=1;j<=n;j++)
{
save[i][j]=st[j-1]-'0';
}
}
//printf("orz\n");
memset(dp,-1,sizeof(dp));
printf("%I64d\n",dfs(1,1));
}
return 0;
}