/*
还有一点要特别注意,每个时间点,锤子都可以随意走动
*/
// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>
using namespace std;
// typedef
typedef __int64 LL;
//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define Z(a,b) ((a)<<(b))
#define Y(a,b) ((a)>>(b))
const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
if(x>y) return x;
return y;
}
template<class T> inline T TMIN(T x,T y)
{
if(x<y) return x;
return y;
}
template<class T> inline T MMAX(T x,T y,T z)
{
return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
return TMIN(TMIN(x,y),z);
}
template<class T> inline void SWAP(T &x,T &y)
{
T t = x;
x = y;
y = t;
}
// code begin
#define offset 5
int N,D,M;
struct node
{
int x;
int y;
int t;
friend bool operator<(node a,node b)
{
return a.t<b.t;
}
};
node data[1010];
int DP[2][32+offset][32+offset];
bool used[32+offset][32+offset];
int ans ;
bool Isin(int r,int c)
{
// offset-D可以到达 N+offset-1+D也可以到达
if( r<0 || r>=N || c<0 || c>=N ) return false;
return true;
}
void extend(int r,int c)
{
for(int i=-D;i<=D;i++)
{
for(int j=-D;j<=D;j++)
{
//
int tmp = DP[(ans+1)&1][r][c];
int rr=r,cc=c,ii=0,jj=0;
while(true)
{
tmp += used[rr][cc];
DP[(ans)&1][rr][cc] = TMAX(DP[(ans)&1][rr][cc],tmp);
ii+=i;
jj+=j;
rr+=i;
cc+=j;
if(!Isin(rr,cc)) break;
if( ii*ii+jj*jj>D*D) break;
if( ii==0 && jj==0) break;
}
}
}
}
int main()
{
read;
write;
int cas = 0;
while(scanf("%d %d %d",&N,&D,&M)==3)
{
if(N+D+M==0) break;
for(int i=0;i<M;i++)
{
scanf("%d %d %d",&data[i].x,&data[i].y,&data[i].t);
data[i].x+=offset;
data[i].y+=offset;
}
sort(data,data+M);
N+=offset*2;
// N棋盘大小 (0,0) - (N-1,N-1);
// (0+offset,0+offset,N+offset-1,N+offset-1);
ans = 0;
memset(DP,0,sizeof(DP));
for(int i=1,j=0;i<=10;i++)
{
//初始化
memset(used,0,sizeof(used));
for( ;j<M;j++)
{
if(data[j].t!=i)
{
break;
}
used[data[j].x][data[j].y] = true;
}
//进行扩展
for(int k=0;k<N;k++)
{
for(int p=0;p<N;p++)
{
extend(k,p);
}
}
if(j==M) break;
ans++;
}
// ans即为最终结果
int ret = 0;
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(DP[(ans)&1][i][j]>ret)
ret = DP[(ans)&1][i][j];
}
}
printf("%d\n",ret);
}
return 0;
}